ios developer tiny share-20160816

来源:互联网 发布:cf领活动枪软件 编辑:程序博客网 时间:2024/06/05 07:19

今天讲在方法中如果存在一个指针类型的local变量,它的生命周期是怎么的?另外,讲下NSLog()方法。

Use Pointers to Keep Track of Objects
C and Objective-C use variables to keep track of values, just like most other programming languages.

There are a number of basic scalar variable types defined in standard C, including integers, floating-point numbers and characters, which are declared and assigned values like this:

    int someInteger = 42;    float someFloatingPointNumber = 3.14f;

Local variables, which are variables declared within a method or function, like this:

- (void)myMethod {    int someInteger = 42;}

are limited in scope to the method in which they are defined.

In this example, someInteger is declared as a local variable inside myMethod; once execution reaches the closing brace of the method, someInteger will no longer be accessible. When a local scalar variable (like an int or a float) goes away, the value disappears too.

Objective-C objects, by contrast, are allocated slightly differently. Objects normally have a longer life than the simple scope of a method call. In particular, an object often needs to stay alive longer than the original variable that was created to keep track of it, so an object’s memory is allocated and deallocated dynamically.

Note: If you’re used to using terms like the stack and the heap, a local variable is allocated on the stack, while objects are allocated on the heap.
This requires you to use C pointers (which hold memory addresses) to keep track of their location in memory, like this:

- (void)myMethod {    NSString *myString = // get a string from somewhere...    [...]}

Although the scope of the pointer variable myString (the asterisk indicates it’s a pointer) is limited to the scope of myMethod, the actual string object that it points to in memory may have a longer life outside that scope. It might already exist, or you might need to pass the object around in additional method calls, for example.


You Can Pass Objects for Method Parameters
If you need to pass along an object when sending a message, you supply an object pointer for one of the method parameters. The previous chapter described the syntax to declare a method with a single parameter:

- (void)someMethodWithValue:(SomeType)value;

The syntax to declare a method that takes a string object, therefore, looks like this:

- (void)saySomething:(NSString *)greeting;

You might implement the saySomething: method like this:

- (void)saySomething:(NSString *)greeting {    NSLog(@"%@", greeting);}

The greeting pointer behaves like a local variable and is limited in scope just to the saySomething: method, even though the actual string object that it points to existed prior to the method being called, and will continue to exist after the method completes.

Note: NSLog() uses format specifiers to indicate substitution tokens, just like the C standard library printf() function. The string logged to the console is the result of modifying the format string (the first argument) by inserting the provided values (the remaining arguments).
There is one additional substitution token available in Objective-C, %@, used to denote an object. At runtime, this specifier will be substituted with the result of calling either the descriptionWithLocale: method (if it exists) or the description method on the provided object. The description method is implemented by NSObject to return the class and memory address of the object, but many Cocoa and Cocoa Touch classes override it to provide more useful information. In the case of NSString, the description method simply returns the string of characters that it represents.
For more information about the available format specifiers for use with NSLog() and the NSString class, see String Format Specifiers.

0 0
原创粉丝点击