《Programming iOS 6》

《preface》

The purpose of this book is to proceed by focusing on the underlying knowledge needed for an actual understanding of iOS programming. That is precisely the opposite of a cookbook. This book has no simple recipes ready for you to drop into your own code and come up with an app. I don’t give you some fish; I teach you what a fish is and what you need to know to obtain one. The number of books out there that skip blithely past the fundamentals, leaving the reader to pick up Objective-C somehow independently, is painfully huge. The result is that numerous learners are encouraged to try their hand at programming when, to judge from the nature of the questions they ask and the confusion they exhibit, they don’t understand what they are doing.

<chart 03>

The tie between a pointer, an instance, and the class of that instance is so close that it is natural to speak of an expression like MyClass* as meaning “a MyClass instance,” and of a MyClass* value as “a MyClass.” A Objective-C programmer will say simply that, in the previous example, sis an NSString, that uppercaseString returns “an NSString,” and so forth.

Declaration without initialization, before the advent of ARC in iOS 5 (Chapter 12), created a dangerous situation:

NSString* s;

空指针不会crash:
NSString* s = nil; // now s is nil
NSString* s2 = [s uppercaseString]; // now s2 is nil

Declaring a Method

The declaration for a method has three parts:

  • Either + or -, meaning that the method is a class method or an instance method, respectively.
  • The data type of the return value, in parentheses.
  • The name of the method, split after each colon. Following each colon is the corresponding parameter, expressed as the data type of the parameter, in parentheses, followed by a placeholder name for the parameter.

So, for example, Apple’s documentation tells us that the declaration for the UIColor class method colorWithRed:green:blue:alpha: is:

+ (UIColor*) colorWithRed: (CGFloat) red green: (CGFloat) green
                     blue: (CGFloat) blue alpha: (CGFloat) alpha
Make very sure you can read this declaration! You should be able to look at it and say to yourself instantly, “The name of this method is colorWithRed:green:blue:alpha: . It’s a class method that takes four CGFloat parameters and returns a UIColor.”

An id is a pointer, so you don’t say id*. It is defined to mean “an object pointer,”。



Chapter 4. Objective-C Classes

//苹果就是简单阿!不像C++,搞一堆乱七八糟的模板阿,boost阿,设计模式阿。。。。
A class can have many subclasses but only one superclass.

no overriding, and no overloading;

Objective-C has no namespaces;

A class method may be called by sending a message directly to the name of a class. Clearly, this is possible because a class is an object, and the name of the class here represents that object.

A class object is not an instance, but it is definitely a full-fledged object. Therefore, a class object can be used wherever an object can be used. 


Chapter 5. Objective-C Instances

Ready-Made Instances;

Instantiation from Scratch;

Initialization;


The keyword self.
The keyword self  does not actually mean “in the same class.” It’s an instance, after all, not a class. What instance? It’s this same instance. The same as what? The same instance to which the message was sent that resulted in the keyword  self  being encountered in the first place.

The Keyword super

The keyword  super  is class-based, and it means: “Start the search for messages I receive in the superclass of this class” (where “this class” is the class where the keyword  super  appears).

Instance Variables and Accessors


By default, instance variables are protected, meaning that other classes (except for subclasses) can’t see them. So if, somewhere else, I instantiate a Dog, I won’t be able to access that Dog instance’s number instance variable.

Key–Value Coding


Objective-C provides a means for translating from a string to an instance variable accessor, calledkey–value coding.

How to Write an Initializer


Chapter 6. Anatomy of an Xcode Project


Chapter 7. Nib Management

 it is based on the file extension .nib, which originated as an acronym for “NeXTStep Interface Builder”.
But a .xib file is still referred to as a nib file.

Indeed, deciding whether to create an interface object in code or through a nib file is not always easy; each approach has its advantages.
 

Nib Loading and File’s Owner

If a nib is designated by the Info.plist key “Main nib file base name” ( NSMainNibFile, see Chapter 6), it is loaded automatically as the app launches; but this is an exceptional case, and has now fallen out of favor — there are no automatically loaded main nib files in the current project templates.

The three most common ways of telling a nib to load are:

initWithNibName:bundle:
This is UIViewController’s designated initializer. It tells the UIViewController to which it is sent to prepare to load the named nib with itself as owner. (That’s what’s happening in the line of code from AppDelegate.m that I quoted just a moment ago.) The moment of actual loading is automatically postponed until the nib is needed. I’ll talk much more about this in Chapter 19.
loadNibNamed:owner:options:
An NSBundle instance method. Usually, you’ll direct it to [NSBundle mainBundle]. Observe that the owner is one of the parameters. I’ll show an example in the next section of this chapter.
instantiateWithOwner:options:
A UINib instance method. The nib in question was specified when UINib was instantiated. Again, the owner is one of the parameters. An example appears in Chapter 21.

Making and Loading a Nib

The File’s Owner top-level object in a nib file is a proxy for the instance that will be the nib’s owner when the nib loads. To form a successful connection between this proxy object and a nib object, the nib needs to know the class of that instance. In the nib editor, you must set the File’s Owner’s class to match the nib owner’s class. Obviously this means that you must know in advance, while editing the nib, what the class of the nib owner will be when the nib loads.

Outlet Connections

A connection is a named unidirectional linkage from one object in a nib file (the connection’s source) to another object in the same nib file (the connection’s target). An outlet is a connection whose name corresponds to an instance variable in the source object.
The File’s Owner top-level object in a nib file is a proxy for the instance that will be the nib’s owner when the nib loads.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值