Miscellanies of iOS 2) Random Possessions

Chapter 2  Objective C                 The Big Nerd Ranch Guide

1. But there is an important difference between a structure in C and a class in Objective-C: a class has methods. A method is similar to a function: it has a name, a return type, and a list of parameters that it expects. A method also has access to an object’s instance variables. If you want an object to run the code in one of its methods, you send that object a message.

2.

Party *partyInstance; 

This variable is named partyInstance. It is meant to be a pointer to an instance of the class Party.However, this does not create a Party instance – only a variable that can point to a Party object.

The first message you always send to a newly allocated instance is an initialization message. Although sending an alloc message to a class creates an instance, the instance isn’t valid until it has been initialized.

Because an object must be allocated and initialized before it can be used,

Party *partyInstance = [[Party alloc] init];


3.

 

4. In Objective-C, the name of a method is what makes it unique. Therefore, a class cannot have two methods with the same name.

5. Also, notice the distinction being made between a message and a method: a method is a chunk of code that can be executed, and a message is the act of asking a class or object to execute a method. The name of a message always matches the name of the method to be executed.

6.  To destroy an object, you set the variable that points at it to nil.                                                     pp69

partyInstance = nil;
This line of code destroys the object pointed to by the partyInstance variable and sets the value of the partyInstance variable to nil. 

(It’s actually a bit more complicated than that, and you’ll learn about the details of memory management in the next chapter.)


A pointer that has a value of nil is typically used to represent the absence of an object.

If you send a message to a variable that is nil, nothing happens. In other languages, sending a message to the zero pointer is illegal

7. NSMutableArray pp72

8. 

NSString *myString = @"Hello, World!";
int len = [myString length];
len = [@"Hello, World!" length];

 9. In C, there is a function called printf that does the same thing. However, NSLog adds one more token to the available list: %@. The type of the argument this token responds to is “any object.” 

When %@ is encountered in the format string, instead of the token being replaced by the corresponding argument, that argument is sent the message description. The description method returns an NSString that replaces the token. Because the argument is sent a message, that argument must be an object. As we’ll see shortly, every object implements the method description, so any object will work.

10. An array is an ordered list of objects, and these objects can be accessed by an index. Other languages might call it a list or a vector. An NSArray is immutable, which means you cannot add or remove objects after the array is instantiated. You can, however, retrieve objects from the array.

11. In Objective-C, an array does not actually contain the objects that belong to it; instead it holds a pointer to each object. When an object is added to an array,

12. Arrays can only hold references to Objective-C objects. This means primitives and C structures cannot be added to an array. For example, you cannot have an array of ints. Also, because arrays hold pointers to objects, a single array can contain objects of different types. This is different from most strongly-typed languages where an array can only hold objects of its declared type. 

13. 

int numberOfObjects = [array count];
14. When an object is added to an array with the message addObject:, it is added at the end of the array. You can also insert objects at a specific index – as long as that index is less than or equal to the current number of objects in the array.

    int numberOfObjects = [array count];    
    [array insertObject:object 
                atIndex:numberOfObjects];
15. Note that you cannot add nil to an array. If you need to add “holes” to an array, you must use NSNull. NSNull is an object that represents nil and is used specifically for this task. 
[array addObject:[NSNull null]]

16. 
NSString *object = [array objectAtIndex:0];
17. As the top superclass, NSObject’s role is to implement the basic behavior of every object in Cocoa Touch. Three of the methods NSObject implements are alloc, init, and description. 

18. 


19. In object-oriented languages, we call methods that get and set instance variables accessors. Individually, we call them getters and setters. Without these methods, an object cannot access the instance variables of another object. 

20. In object-oriented languages, we call methods that get and set instance variables accessors. Individually, we call them getters and setters. Without these methods, an object cannot access the instance variables of another object. 

21. But in Objective-C, you cannot have two methods with the same selector and different return types (or arguments)

22. When you send a message to super, you are sending a message to self, but the search for the method skips the object’s class and starts at the superclass.

23. some simple rules for initializers.  

 a) A class inherits all initializers from its superclass and can add as many as it wants for its own purposes. 
 b) Each class picks one initializer as its designated initializer. 
 c) The designated initializer calls the superclass’s designated initializer. 
 d) Any other initializer a class has calls the class’s designated initializer. 
 e) If a class declares a designated initializer that is different from its superclass, the superclass’s designated initializer must be overridden to call the new designated initializer.

24. One common use for class methods is to provide convenient ways to create instances of that class. 

25. 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.

26. Fast Enumeration

Objective-C 2.0 introduced fast enumeration. 

for (BNRItem *item in items) {    
	NSLog(@"%@", item);	
}

27. Objective-C has no notion of namespaces. Instead, we prefix class names with two or three letters to keep them distinct. For example, in this exercise, the class was named BNRItem instead of Item.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值