c一些学习过程中突然错过的细节

Description A UIScreen object defines the properties associated with a hardware-based display. iOS devices have a main screen and zero or more attached screens. Use this class to obtain screen objects for each display attached to the device. Each screen object defines the bounds rectangle for the associated display and other interesting properties such as its brightness.
Availability iOS (2.0 and later)
Declared In UIScreen.h
Reference UIScreen Class Reference
- (void)loadView
{
    //1
    //BNRHypnosisView *backgroundView = [[BNRHypnosisView alloc] init];
    
    //2
    CGRect frame = [UIScreen mainScreen].bounds;
    BNRHypnosisView *backgroundView = [[BNRHypnosisView alloc] initWithFrame:frame];
    
    CGRect textFieldRect = CGRectMake(40, 70, 230, 30);
    UITextField *textField = [[UITextField alloc] initWithFrame:textFieldRect];
    [backgroundView addSubview:textField];
        self.view = backgroundView;
}

之前没有使用2也可以。当时还很疑惑,为什么书上直接就初始化一个视图backgroundView给视图控制器。

UIScreen是什么?


    [[UIApplication sharedApplication] scheduleLocalNotification:note];

调用UIApplication 单例对象的 scheduleLocalNotification:注册该通知note

单例对象是什么?

 p165


- (instancetype)initWithStyle:(UITableViewStyle)style
{
    return [self init];  
}

Designated initializer missing a 'super' call to a designated initializer of the super class.

Designated initializer should only invoke a designated initializer on 'super'.

根据BNR更换指定初始化方法的过程中,Xcode给的警告。

不要搞晕了头


mvc 其中的c是比较重要的。因为它是模型和数据的桥梁。

比如视图被视图控制器控制,同时视图控制器更是视图的委托对象,因而遵守协议,在协议里面把模型数据得以展示在视图上面。

因此模型有自己相对独立的设计模式。比如展示数据和隐私数据。隐私数据放在类扩展里面。

吊诡的两端代码 


// 通过数据源对象获得显示的行数
//- (NSInteger)tableview:(UITableView *)tableView numberOfRowsInsection:(NSInteger)section
//{
//    NSLog(@"numberOfRowsInsection");
//    return [[[BNRItemStore sharedStore] allItems] count];
//}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSLog(@"numberOfRowsInsection");
    return [[[BNRItemStore sharedStore] allItems] count];
}

居然有一个拼写错误。这能原谅吗?

表格 移动行 Moving Rows


- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
    NSLog(@"lalalalalalalala");
    
    // 即使不掉用 该数据模型的方法,也可以实现视图的表格行的移动
    [[BNRItemStore sharedStore] moveItemAtIndex:sourceIndexPath.row
                                        toIndex:destinationIndexPath.row];
}

但是数据根本就没有改变。所以目前来讲,还是没有什么影响的。

copy的一个坑


BNRItem.h

@interface BNRItem : NSObject

@property (nonatomic, copy) NSString *itemName;

+ (instancetype)initWithItemName:(NSString *)name;

@end

BNRItem.m

@implementation BNRItem

+ (instancetype)initWithItemName:(NSString *)name
{
    self = [super self];

    if (self){
        _itemName = name; //此处压根就不会调用 itemName 存放法
        // [self setItemName:name]; //copy 特性触发
    }

    return self;
}

- (NSString *)description
{
    return [NSString stringWithFormat:@"%@", self.itemName];
}

@

main.m

@autoreleasepool{

    NSMutableString *mutableString = [NSMutableSring stringWithFormat:@"abc"];
    BNRItem *item = [[BNRItem alloc] initWithItemName:mutableString]];

    [mutableString appendFormat:@"def"];

    NSLog(@"%@", item);

    NSLog(@"%@", mutableString);

}

后来再试试点语法:

self.itemName = name;

也能起到效果。那么问题来了,到底在初始化方法里面使用存取方法还是直接操作实例变量?

self 初始化方法


Notice the use of self in randomItem.
Because randomItem is a class method,
self refers to the BNRItem class itself instead of an instance.
Class methods should use self in convenience methods instead of their class name so that a subclass can be sent the same message.
In this case,
if you create a subclass of BNRItem called BNRToxicWasteItem,
you could do this:
BNRToxicWasteItem *item = [BNRToxicWasteItem randomItem];

 更早一点的版本里面这样措辞道:

Notice the use of self in randomItem.
Because randomItem is a class method,
self refers to the BNRItem class itself instead of an instance.
Class methods should use self in convenience methods instead of their class name so that a subclass can be sent the same message.
In this case,
if you create a subclass of BNRItem,
you can send that subclass the message randomItem.
Using self (instead of BNRItem) will allocate an instance of the class that was sent the message and set the instance’s isa pointer to that class.

 原来的橙色部分被改换为具体的某个例子,而忽略掉理论的讲解,可能更理论的东西是不属于iOS编程的范畴,作者不愿意让读者来了解这些背后的复杂度。就算了解了,也不应该在这本书里面得到。

我的翻译是:

“使用self将会分配一个类的实例,这个类是被发送消息的类,且实例的isa指针将会设置为这个类本身。”

原文地址:https://www.cnblogs.com/dotdog/p/4741882.html