IOS documentation 01、02、03 Encapsulating Data - XinliWang/iOS GitHub Wiki
####1.NSString and NSNumber
all basic NSString and NSNumber objects are immutable. If you need to represent a different number, you must use a new NSNumber instance.
####2.method declaration
C function: void SomeFunction(SomeType value);
Objective C: (void) someMethodWithValue:(SomeType)value;
(void) someMethodWithFirstValue:(SomeType)value1 secondValue:(AnotherType)value2;
####3.interface and implements
interface for a class:
@interface XYZPerson : NSObject
-(void)sayHello;
@end
implementation of that class:
@implementation XYZPerson
-(void)sayHello{
NSLog(@"Hello, world!")
}
@end
####4.Use Pointerss to keep track of objects Local variable,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. #####If you are used to using terms like the stack and heap, a local variable is allocated on the stack, while objects are allocated on the heap. NSLog(@"%@",greeting), just like c standard library printf() function
-(void)myMethod{
NSString *myString = //get a string
[...]
}
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.
@implementation XYZPerson
- (void)sayHello {
[self saySomething:@"Hello, world!"];
}
- (void)saySomething:(NSString *)greeting {
NSLog(@"%@", greeting);
}
@end
####5.Allocate and initialize The correct way to allocate and initialize an object is to nest the alloc call inside the call to init, like this:
NSObject *newObject = [[NSObject alloc] init];
It is the same as:
NSObject *newObject = [NSObject new];
Initialization methods with arguments are called in just the same way as plain init methods—an NSNumber object is allocated and initialized like this:
NSNumber *magicNumber = [[NSNumber alloc] initWithInt:42];
####6.< 、> 、== '==' :
if([firstPerson isEqual:secondPerson]){
//firstPerson is identical to secondPerson
}
''<'' 、">":
if([someDate compare:anotherDate] == NSSOrderedAscending){
//someDate is earlier than anotherDate
}
####7. Dot Syntax
//the same as using [somePerson firstName]
NSString *firstName = sosmeePerson.firstName;
//[somePerson setFirstName:@"Johhnny"]
somePerson.firstName = @"Johhnny";
In this example, it’s clear that myString is a local variable and _someString is an instance variable.
- (void)someMethod {
NSString *myString = @"An interesting string";
_someString = myString;
}
In general, you should use accessor methods or dot syntax for property access even if you’re accessing an object’s properties from within its own implementation, in which case you should use self:
- (void)someMethod {
NSString *myString = @"An interesting string";
self.someString = myString;
//or [self setSomeString:myString];
####8.week reference and Unsafe reference An unsafe reference is similar to a weak reference in that it doesn’t keep its related object alive, but it won’t be set to nil if the destination object is deallocated. This means that you’ll be left with a dangling pointer to the memory originally occupied by the now deallocated object, hence the term “unsafe.” Sending a message to a dangling pointer will result in a crash.
@property (unsafe_unretained) NSObject *unsafeProperty;
NSDate * __weak originalDate = self.lastModificationDate;
self.lastModificationDate = [NSDate date];