{"id":11,"date":"2008-07-15T19:15:22","date_gmt":"2008-07-15T23:15:22","guid":{"rendered":"httpss:\/\/www.powenko.com\/blog\/?p=11"},"modified":"2008-07-19T13:54:21","modified_gmt":"2008-07-19T17:54:21","slug":"iphone-objective-c-learning-1-beginning","status":"publish","type":"post","link":"https:\/\/www.powenko.com\/wordpress\/?p=11","title":{"rendered":"iphone, objective-c, learning 1, Beginning"},"content":{"rendered":"<p><strong>id<\/strong><br \/>\nid anObject;<br \/>\nThis type is defined as a pointer to an object<a name=\"\/\/apple_ref\/doc\/uid\/TP30001163-CH11-DontLinkElementID_297\"><\/a>\u2014in reality,<br \/>\n<strong>nil<\/strong><br \/>\nNULL<br \/>\n<strong>[&#8230;]<\/strong><br \/>\nobject messaging<br \/>\n[receiver message]<br \/>\n[myRect display];<br \/>\n[myRect setWidth:20.0];<br \/>\n[myRect setOrigin:30.0 :50.0]; \/\/ This is a bad example of multiple arguments<br \/>\n[receiver makeGroup:group, memberOne, memberTwo, memberThree];<br \/>\nBOOL isFilled = [myRect isFilled];  \/\/ if  myRect filled returns YES,<br \/>\n[receiver makeGroup:group, memberOne, memberTwo, memberThree];<br \/>\n[myRect setPrimaryColor:[otherRect primaryColor]];<\/p>\n<p><strong>.<br \/>\nDot Syntax<\/strong><br \/>\nmyInstance.value = @&#8221;New value&#8221;;  \/\/[myInstance setValue:@&#8221;New value&#8221;];<br \/>\nNSLog(@&#8221;myInstance value: %@&#8221;, myInstance.value); \/\/NSLog(@&#8221;myInstance value: %@&#8221;, [myInstance value]);<\/p>\n<p><strong>string<\/strong><\/p>\n<pre>NSString*  myString = @\"My String\\n\";\r\nNSString*  anotherString = [NSString stringWithFormat:@\"%d %s\", 1, @\"String\"];\r\n\r\n\/\/ Create an Objective-C string from a C string\r\nNSString*  fromCString = [NSString stringWithCString:\"A C string\" encoding:NSASCIIStringEncoding];<\/pre>\n<p><strong>Listing 1-1  Accessing properties using the dot syntax<\/strong><br \/>\nGraphic *graphic = [[Graphic alloc] init];<\/p>\n<p>NSColor *color = graphic.color;<br \/>\nCGFloat xLoc = graphic.xLoc;<br \/>\nBOOL hidden = graphic.hidden;<br \/>\nint textCharacterLength = graphic.text.length;<\/p>\n<p>if (graphic.textHidden != YES) {<br \/>\ngraphic.text = @&#8221;Hello&#8221;;<br \/>\n}<br \/>\ngraphic.bounds = NSMakeRect(10.0, 10.0, 20.0, 120.0)<br \/>\n<strong> Listing 1-2  Accessing properties using accessor methods, as same as Listing 1-1<\/strong><br \/>\nGraphic *graphic = [[Graphic alloc] init];<\/p>\n<p>NSColor *color = [graphic color];<br \/>\nCGFloat xLoc = [graphic xLoc];<br \/>\nBOOL hidden = [graphic hidden];<br \/>\nint textCharacterLength = [[graphic text] length];<\/p>\n<p>if ([graphic isTextHidden] != YES) {<br \/>\n[graphic setText:@&#8221;Hello&#8221;];<br \/>\n}<br \/>\n[graphic setBounds:NSMakeRect(10.0, 10.0, 20.0, 120.0)];<\/p>\n<p><strong>Listing 1-3<\/strong><br \/>\nNSMutableData *data = [NSMutableData dataWithLength:1024];<br \/>\ndata.length += 1024;   \/\/[data setLength:[data length] + 1024];<br \/>\ndata.length *= 2;   \/\/[data setLength:[data length] * 2];<br \/>\ndata.length \/= 4;   \/\/[data setLength:[data length] \/ 4];<\/p>\n<p><strong> Listing 1-4<\/strong><br \/>\n\/\/ each member of the path is an object<br \/>\nx = person.address.street.name; \/\/ x = [[[person address] street] name];<\/p>\n<p>\/\/ the path contains a C struct<br \/>\n\/\/ will crash if window is nil or -contentView returns nil<br \/>\ny = window.contentView.bounds.origin.y;  \/\/ y = [[window contentView] bounds].origin.y;<\/p>\n<p>\/\/ an example of using a setter&#8230;.<br \/>\nperson.address.street.name = @&#8221;Oxford Road&#8221;; \/\/ [[[person address] street] setName: @&#8221;Oxford Road&#8221;];<\/p>\n<p><strong>Self<\/strong><br \/>\nself.age = 10;<\/p>\n<p><strong>Key-value coding (KVC)<\/strong><br \/>\n<em> Sample: define a class<\/em><br \/>\n@interface MyClass<br \/>\n@property NSString *stringProperty;<br \/>\n@property NSInteger integerProperty;<br \/>\n@property MyClass *linkedInstance;<br \/>\n@end<\/p>\n<p><em>Used the class in below, sample 1<\/em><br \/>\nMyClass *myInstance = [[MyClass alloc] init];<br \/>\nNSString *string = [myInstance valueForKey:@&#8221;stringProperty&#8221;];<br \/>\n[myInstance setValue:[NSNumber numberWithInt:2] forKey:@&#8221;integerProperty&#8221;];<\/p>\n<p><span style=\"color: #000080;\"><em>sample2<\/em><\/span><br \/>\nMyClass *anotherInstance = [[MyClass alloc] init];<br \/>\nmyInstance.linkedInstance = anotherInstance;<br \/>\nmyInstance.linkedInstance.integerProperty = 2;<\/p>\n<p><span style=\"color: #000080;\"><em> sample3: This has the same result as:<\/em><\/span><br \/>\nMyClass *anotherInstance = [[MyClass alloc] init];<br \/>\nmyInstance.linkedInstance = anotherInstance;<br \/>\n[myInstance setValue:[NSNumber numberWithInt:2] forKeyPath:@&#8221;linkedInstance.integerProperty&#8221;];<br \/>\naVariable = anObject.aProperty;<br \/>\nanObject.name = @&#8221;New Name&#8221;;<br \/>\nxOrigin = aView.bounds.origin.x;<br \/>\nNSInteger i = 10;<br \/>\nanObject.integerProperty = anotherObject.floatProperty = ++i;<\/p>\n<h3>Class<\/h3>\n<p><a href=\"httpss:\/\/www.powenko.com\/blog\/wp-content\/uploads\/2008\/07\/graphichierarchy.gif\"><img loading=\"lazy\" decoding=\"async\" class=\"alignleft size-medium wp-image-12\" title=\"graphichierarchy\" src=\"httpss:\/\/www.powenko.com\/blog\/wp-content\/uploads\/2008\/07\/graphichierarchy-300x165.gif\" alt=\"\" width=\"300\" height=\"165\" \/><\/a><\/p>\n<p>Rectangle *myRect;<br \/>\nGraphic *myRect;<\/p>\n<p>if ( [anObject isMemberOfClass:someClass] )<br \/>\nif ( [anObject isKindOfClass:someClass] )<\/p>\n<p><strong>Class Objects<\/strong><br \/>\nint versionNumber = [Rectangle version];<br \/>\nid aClass = [anObject class];<br \/>\nid rectClass = [Rectangle class];<br \/>\nClass aClass = [anObject class];<br \/>\nClass rectClass = [Rectangle class];<\/p>\n<p><strong>Creating Instances<\/strong><br \/>\nid  myRect;<br \/>\nmyRect = [Rectangle alloc];  \/\/ dynamically allocates memor<br \/>\nmyRect = [[Rectangle alloc] init];  \/\/same as,  myRect = [Rectangle alloc];<br \/>\n[myMatrix setCellClass:[NSButtonCell class]];<\/p>\n<h3>Class Object<\/h3>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignleft size-medium wp-image-13\" title=\"cellhierarchy\" src=\"httpss:\/\/www.powenko.com\/blog\/wp-content\/uploads\/2008\/07\/cellhierarchy-300x109.gif\" alt=\"\" width=\"300\" height=\"109\" \/><\/p>\n<p><strong>create Class Object <\/strong><br \/>\nint MCLSGlobalVariable;<br \/>\n@implementation MyClass<br \/>\n\/\/ implementation continues<\/p>\n<p>static MyClass *MCLSSharedInstance;<br \/>\n@implementation MyClass<br \/>\n+ (MyClass *)sharedInstance<br \/>\n{<br \/>\n\/\/ check for existence of shared instance<br \/>\n\/\/ create if necessary<br \/>\nreturn MCLSSharedInstance;<br \/>\n}<br \/>\n\/\/ implementation continues<\/p>\n<p><strong> Initializing<\/strong><br \/>\n+ (void)initialize<br \/>\n{<br \/>\nstatic BOOL initialized = NO;<br \/>\nif (!initialized) {<br \/>\n\/\/ Perform initialization here.<br \/>\n&#8230;<br \/>\ninitialized = YES;<br \/>\n}<br \/>\n}<\/p>\n<p><strong>Class Names in Source Cod<\/strong><br \/>\nRectangle *anObject;<br \/>\nif ( [anObject isKindOfClass:[Rectangle class]] )<br \/>\nNSString *className;<br \/>\nif ( [anObject isKindOfClass:NSClassFromString(className)] )<br \/>\n<strong>Defining a Class<\/strong><\/p>\n<p><strong>Class Interface<\/strong><br \/>\n<em>solution 1: <\/em><br \/>\n#import &#8220;ItsSuperclass.h&#8221;<\/p>\n<p>@interface ClassName : ItsSuperclass<br \/>\n{<br \/>\ninstance variable declarations<br \/>\n}<br \/>\nmethod declarations<br \/>\n@end<\/p>\n<p><em>solution 2:<\/em><br \/>\n#import &#8220;ClassName.h&#8221;<\/p>\n<p>@implementation ClassName<br \/>\nmethod definitions<br \/>\n@end<\/p>\n<p>+ alloc{    &#8230;}<br \/>\n&#8211; (BOOL)isfilled{    &#8230;}<br \/>\n&#8211; (void)setFilled:(BOOL)flag{    &#8230;}<\/p>\n<p>#import<br \/>\n&#8211; getGroup:group, &#8230;<br \/>\n{  va_list ap;<br \/>\nva_start(ap, group);<br \/>\n}<\/p>\n<p><strong>Referring to Instance Variables<\/strong><br \/>\n&#8211; (void)setFilled:(BOOL)flag<br \/>\n{<br \/>\nfilled = flag;<br \/>\n&#8230;<br \/>\n}<\/p>\n<p>@interface Sibling : NSObject<br \/>\n{<br \/>\nSibling *twin;<br \/>\nint gender;<br \/>\nstruct features *appearance;<br \/>\n}<\/p>\n<p>&#8211; makeIdenticalTwin<br \/>\n{<br \/>\nif ( !twin ) {<br \/>\ntwin = [[Sibling alloc] init];<br \/>\ntwin-&gt;gender = gender;<br \/>\ntwin-&gt;appearance = appearance;<br \/>\n}<br \/>\nreturn twin;<br \/>\n}<\/p>\n<p>&#8211; (BOOL)isFilled<br \/>\n{<br \/>\nreturn filled;<br \/>\n}<\/p>\n<p><strong>Referring to Other Classes<\/strong><br \/>\n@class Rectangle, Circle;<br \/>\n&#8211; (void)setPrimaryColor:(NSColor *)aColor;<\/p>\n<table class=\"graybox\" border=\"0\" cellspacing=\"0\" cellpadding=\"5\">\n<tbody>\n<tr>\n<th align=\"left\" scope=\"col\">Directive<\/th>\n<th align=\"left\" scope=\"col\">Meaning<\/th>\n<\/tr>\n<tr>\n<td scope=\"row\"><code>@private<\/code><\/td>\n<td>The instance variable<a name=\"\/\/apple_ref\/doc\/uid\/TP30001163-CH12-DontLinkElementID_82\"><\/a> is accessible only within the class that declares it.<\/td>\n<\/tr>\n<tr>\n<td scope=\"row\"><code>@protected<\/code><\/td>\n<td>The instance variable is accessible within the class that declares it and within classes that inherit it.<\/td>\n<\/tr>\n<tr>\n<td scope=\"row\"><code>@public<\/code><\/td>\n<td>The instance variable is accessible everywhere.<\/td>\n<\/tr>\n<tr>\n<td scope=\"row\"><code>@package<\/code><\/td>\n<td>On 64-bit, an <code>@package<\/code> instance variable acts like <code>@public<\/code> inside the image that implements the class, but <code>@private<\/code> outside. This is analogous to <code>private_extern<\/code> for variables and functions. Any code outside the class implementation\u2019s image that tries to use the instance variable will get a link error. This is most useful for instance variables in framework classes, where <code>@private<\/code> may be too restrictive but <code>@protected<\/code> or <code>@public<\/code> too permissive.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>@interface Worker : NSObject<br \/>\n{<br \/>\nchar *name;<br \/>\n@private<br \/>\nint age;<br \/>\nchar *evaluation;<br \/>\n@protected<br \/>\nid job;<br \/>\nfloat wage;<br \/>\n@public<br \/>\nid boss;<br \/>\n}<\/p>\n<p>&#8211; promoteTo:newPosition<br \/>\n{<br \/>\nid old = job;<br \/>\njob = newPosition;<br \/>\nreturn old;<br \/>\n}<\/p>\n<p>Worker *ceo = [[Worker alloc] init];<br \/>\nceo-&gt;boss = nil;<\/p>\n<h3>Categories and Extensions<\/h3>\n<p>#import &#8220;ClassName+CategoryName.h&#8221;<\/p>\n<p>@implementation ClassName ( CategoryName )<br \/>\n\/\/ method definitions<br \/>\n@end<\/p>\n<p><strong>Extensions<\/strong><br \/>\n@interface MyObject : NSObject<br \/>\n{    NSNumber *number;<br \/>\n}<br \/>\n&#8211; (NSNumber *)number;<br \/>\n@end<\/p>\n<p>@interface MyObject (Setter)<br \/>\n&#8211; (void)setNumber:(NSNumber *)newNumber;<br \/>\n@end<\/p>\n<p>@implementation MyObject<br \/>\n&#8211; (NSNumber *)number<br \/>\n{    return number;<br \/>\n}<br \/>\n@end<\/p>\n<p>@interface MyObject : NSObject<br \/>\n{   NSNumber *number;<br \/>\n}<br \/>\n&#8211; (NSNumber *)number;<br \/>\n@end<\/p>\n<p>@interface MyObject ()<br \/>\n&#8211; (void)setNumber:(NSNumber *)newNumber;<br \/>\n@end<\/p>\n<p>@implementation MyObject<br \/>\n&#8211; (NSNumber *)number<br \/>\n{    return number;<br \/>\n}<br \/>\n&#8211; (void)setNumber(NSNumber *)newNumber<br \/>\n{    number = newNumber;<br \/>\n}<br \/>\n@end<br \/>\n<strong>Declared Properties<\/strong><br \/>\n<em>Declaring properties in a class<\/em><br \/>\n\/\/ MyClass.h<br \/>\n@interface MyClass : NSObject<br \/>\n{<br \/>\nNSString *value;<br \/>\nNSTextField *textField;<br \/>\n@private<br \/>\nNSDate *lastModifiedDate;<br \/>\n}<br \/>\n@property(copy, readwrite) NSString *value;<br \/>\n@property(retain) IBOutlet NSTextField *textField;<br \/>\n@end<\/p>\n<p>\/\/ MyClass.m<br \/>\n\/\/ Class extension to declare private property<br \/>\n@interface MyClass ()<br \/>\n@property(retain) NSDate *lastModifiedDate;<br \/>\n@end<\/p>\n<p>@implementation MyClass<br \/>\n@synthesize value;<br \/>\n@synthesize textField;<br \/>\n@synthesize lastModifiedDate;<br \/>\n\/\/ implementation continues<br \/>\n@end<\/p>\n<p><strong>Using Properties<\/strong><br \/>\n&#8211; (NSString *)name;<br \/>\n&#8211; (void)setName:(NSString *)newName;  \/\/ same as, @property NSString *name;<\/p>\n<p>@interface MyClass : NSObject<br \/>\n{    NSString *value;<br \/>\n}<br \/>\n@property(copy, readwrite) NSString *value;<br \/>\n@end<\/p>\n<p>@implementation MyClass<br \/>\n@synthesize value;<br \/>\n@end<\/p>\n<p>@interface MyClass : NSObject<br \/>\n{<br \/>\nNSString *value;<br \/>\n}<br \/>\n@property(copy, readwrite) NSString *value;<br \/>\n@end<\/p>\n<p>\/\/ assume using garbage collection<br \/>\n@implementation MyClass<br \/>\n@dynamic value;<br \/>\n&#8211; (NSString *)value {<br \/>\nreturn value;<br \/>\n}<br \/>\n&#8211; (void)setValue:(NSString *)newValue {<br \/>\nif (newValue != value) {<br \/>\nvalue = [newValue copy];<br \/>\n}<br \/>\n}<br \/>\n@end<\/p>\n<p><em>getter=getterName, setter=setterName<\/em><br \/>\ngetter= and setter= specify respectively the names of get and set accessors for the property. The getter must return a type matching the property\u2019s type and take no arguments. The setter method must take a single argument of a type matching the property\u2019s type and must return void.<\/p>\n<p><em>readonly<\/em><br \/>\nIndicates that the property is read-only. The default is read\/write.<\/p>\n<p><em>readwrite<\/em><br \/>\nIndicates that the property should be treated as read\/write. This is the default.<\/p>\n<p><em>assign<\/em><br \/>\nSpecifies that the setter uses simple assignment. This is the default.<\/p>\n<p><em>retain<\/em><br \/>\nSpecifies that retain should be invoked on the object upon assignment. (The default is assign.)<\/p>\n<p><em>copy<\/em><br \/>\nSpecifies that a copy of the object should be used for assignment. (The default is assign.)<\/p>\n<p><em>nonatomic<\/em><br \/>\nSpecifies that accessors are non-atomic. By default, accessors are atomic.( There is no keyword to denote atomic.)<\/p>\n<p><strong>Property Re-declaration<\/strong><br \/>\n@interface MyObject : NSObject<br \/>\n{   NSString *language;<br \/>\n}<br \/>\n@property (readonly, copy) NSString *language;<br \/>\n@end<\/p>\n<p>\/\/ private implementation file<br \/>\n@interface MyObject ()<br \/>\n@property (readwrite, copy) NSString *language;<br \/>\n@end<\/p>\n<p>@implementation MyObject<br \/>\n@synthesize language;<br \/>\n@end<\/p>\n<p>@property (nonatomic, copy) NSString *string;<br \/>\n-(void)setString:(NSString *)newString<br \/>\n{<br \/>\nif (string != newString) {<br \/>\n[string release];<br \/>\nstring = [newString copy];<br \/>\n}<br \/>\n}<\/p>\n<p>@interface MyClass : NSObject<br \/>\n{<br \/>\nNSMutableArray *myArray;<br \/>\n}<br \/>\n@property (nonatomic, copy) NSMutableArray *myArray;<br \/>\n@end<\/p>\n<p>@implementation MyClass<\/p>\n<p>@synthesize myArray;<\/p>\n<p>&#8211; (void)setMyArray:(NSMutableArray *)newArray<br \/>\n{<br \/>\nif (myArray != newArray) {<br \/>\n[myArray release];<br \/>\nmyArray = [newArray mutableCopy];<br \/>\n}<br \/>\n}<\/p>\n<p>@end<\/p>\n<p><strong>Property Declaration<\/strong><br \/>\n@interface MyClass : NSObject<br \/>\n{<br \/>\nNSString *value;<br \/>\n}<br \/>\n@property(copy, readwrite) NSString *value;<br \/>\n@end<\/p>\n<p>\/\/ assume using garbage collection<br \/>\n@implementation MyClass<br \/>\n@dynamic value;<\/p>\n<p>&#8211; (NSString *)value {<br \/>\nreturn value;<br \/>\n}<\/p>\n<p>&#8211; (void)setValue:(NSString *)newValue {<br \/>\nif (newValue != value) {<br \/>\nvalue = [newValue copy];<br \/>\n}<br \/>\n}<br \/>\n@end<\/p>\n<p>@interface MyClass : NSObject<br \/>\n{<br \/>\nNSMutableArray *myArray;<br \/>\n}<br \/>\n@property (nonatomic, copy) NSMutableArray *myArray;<br \/>\n@end<\/p>\n<p>@implementation MyClass<\/p>\n<p>@synthesize myArray;<\/p>\n<p>&#8211; (void)setMyArray:(NSMutableArray *)newArray<br \/>\n{<br \/>\nif (myArray != newArray) {<br \/>\n[myArray release];<br \/>\nmyArray = [newArray mutableCopy];<br \/>\n}<br \/>\n}<\/p>\n<p>@end<\/p>\n<p><strong>Sample Class 1:<\/strong><br \/>\n@interface MyClass : NSObject<br \/>\n{<br \/>\n\tint \t\tcount;<br \/>\n\tid  \t\tdata;<br \/>\n\tNSString*\tname;<br \/>\n}<br \/>\n-(id)initWithString: (NSString*) aName;<br \/>\n+(MyClass*)createMyClassWithString: (NSString*)aName;<br \/>\n@end<\/p>\n<p>@implementation MyClass<br \/>\n&#8211; (id)initWithString:(NSString *) aName<br \/>\n{<br \/>\n    if (self = [super init]) {<br \/>\n        count count = 0;<br \/>\n        data = nil;<br \/>\n        name = [aName copy];<br \/>\n        return self;<br \/>\n    }<br \/>\n}<\/p>\n<p>+ (MyClass *)createMyClassWithString: (NSString *) aName<br \/>\n{<br \/>\n    return [[[self alloc] initWithString:aName] autorelease];<br \/>\n}<br \/>\n@end<\/p>\n<p><strong>reference:<\/strong><br \/>\nhttpss:\/\/developer.apple.com\/iphone\/library\/documentation\/Cocoa\/Conceptual\/ObjectiveC\/Introduction\/chapter_1_section_1.html<br \/>\nObjective begin tutor<br \/>\nhttpss:\/\/freeborn.wordpress.com\/2008\/03\/10\/iphone-developmenet-where-to-start\/<br \/>\nhttpss:\/\/theocacao.com\/document.page\/510<\/p>\n","protected":false},"excerpt":{"rendered":"<p>id id anObject; This type is defined as a pointer to an [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-11","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/www.powenko.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/11"}],"collection":[{"href":"https:\/\/www.powenko.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.powenko.com\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.powenko.com\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.powenko.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=11"}],"version-history":[{"count":0,"href":"https:\/\/www.powenko.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/11\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.powenko.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=11"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.powenko.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=11"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.powenko.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=11"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}