在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
在我们学习一门新的语言时,总要把它和我们熟悉的语言对比着来学习,就象学习英语时,都要记单词的汉语意思,来帮助我们对单词的理解和记忆。 Objective-C是在C的基础上加入了面向对象的特性,而C#是由C和C++派生而来,可见C#显得更“高级”一些. Message Expression [reciever message] [reciever message:argument] [reciever message:argument andArg:arg2] Metho definition - (void)castBallot; - (int)arg; - (void)setArg:(int)age; - (void)registerForState:(NSString*)state party:(NSString*)party; 对于C#开发人员,这样的代码看起来很诡异,别紧张,把它想象成方法调用就对了。 Objective-C 2.0 introduced dot syntax float height = [persion height]; float height = persion.height; [persion setHeight:newHeight]; persion.height = newHeight; [[persion child] setHeight:newHeight]; persion.child.height = newHeight; 也许是为了ObjC看上去不那么“诡异”,终于看上去“正常”了一些吧~! id anObject; Persion *aPersion = (Persion *)anObject;
if(persion == nil) return; if(!persion) return; persion = nil; [button setTarget: nil]; persion = nil; [persion castBallot];
BOOL flag = NO;
SEL action = [button action]; [button setAction:@selector(start:)]; Conceptually similar to function pointer - (void)setName:(NSString*)name age:(int)age; SEL sel = @selector(setName:age:);
Class myClass = [myObject class]; NSLog(@"My class is %@", [myObject className]); if ([myObject isKindOfClass:[UIControl class]]) { // something } if ([myObject isMemberOfClass:[NSString class]]) { // someting string specific } C#中有Type, 上面的代码在C#中如下: Type myClass = myObject.GetType(); Console.WriteLine(string.Format("My class is {0}", myClass.TypeName)); if(myObject.GetType() == typeof(UIControl)) { // something } if (myObject is typeof(String)) { // someting string specific }
if (object1 == ojbect2) { NSLog(@"Same exact object instance"); } Equality - testing object attributes if ([object1 isEqual: object2]) { NSLog(@"Logically equivalent, but may be different object instances"); } C#中好象没有直接比较对象属性值的功能,得自已override Equals方法。 description: - (NSString*)description; [NSString stringWithFormat: @"The answer is: %@", myObject]; NSLog([anObject description]); NSObject 对等于C#中的Object类。 NSString In C constant strings are "simple" In ObjC constant strings are @"just as simple" NSString *aString = @"Hello World!"; NSLog(@"I am a %@, I have %d items", [array className], [array count]); NSString *myString = @"Hello"; NSString *fullString; fullString = [myString stringByAppendString:@" world!"]; fullString would be set to "Hello World!". - (BOOL) isEqualToString:(NSStirng*)string; - (BOOL) hasPrefix:(NSString*)string; - (int)intValue; - (double)doubleValue;
+ (id)string; - (void)appendString:(NSString*)string; - (void)appendFormat:(NSString*)format, ...; NSMutableString *newString = [NSMutableString string]; [newString appendString:@"Hi"]; [newString appendFormat:@" , my favorite number is: %d", [self favoriteNumber]]; 是不是很象StringBuilder? Collections Array, Dictionary, Set Common NSArray methods: + arrayWithObjects:(id)firstObj, ...; //nil terminated!!! - (unsigned)count; - (id)objectAtIndex:(unsigned)index; - (unsigned)indexOfObject:(id)object; NSNotFound returned for index if not found if ([array indexOfObject:@"Purple"] == NSNotFound) { NSLog(@"No color purple"); } NSNotFound不就是-1吗?! NSMutableArray NSMutalbeArray subclasses NSArray. Common NSDictionary methods: + dictionaryWithObjectsAndKeys:(id)firstObject, ...; - (unsigned)count; - (id)objectForKey:(id)key; nil returned if no object found for gived key. NSMutalbeDictionary NSMutalbeDictionary subclasses NSDictionary. Common NSSet methods: + setWithOjbects:(id)firstObj, ...; //nil terminated - (unsigned)count; - (BOOL)containsObject:(id)object; No object is ever in there more than once. NSMutableSet NSMutableSet subclasses NSSet C#好象没Set这样的东东,怎么没什么印象呢?!不过看样子搞一个也不是很麻烦。 Enumeration for (Persion *persion in array) { NSLog([persion description]); } C#的foreach就对了。 Common NSNumber methods: + (NSNumber*)numberWithInt:(int)value; + (NSNumber*)numberWithDouble:(double)value; - (int)intValue; - (double)doubleValue;
#import <Foundation/Foundation.h> @interface Persion : NSObject { // instance variables NSString *name; int arg; } // method declarations - (NSString *)name; - (void)setName:(NSString *)value; - (int)age; - (void)setAge:(int)age; - (BOOL)canLegallyVote; - (void)castBallot; @end Class Implementation write in .m file #import "Persion.h" @implemetation Person - (int)age { return age; } - (void)setAge:(int)value { age = value; } // ... and other methods - (BOOL)canLegallyVote { return ([self age] >= 18); } - (void)castBallot { if([self canLegallyVote]) { // do voting stuff } else { NSLog(@"I'm not allowed to vote!"); } } @end
- (void)doSomething { // Call superclass implementation first [super doSometing]; // Then do our custom behavior int foo = bar; // ... }
Persion *persion = nil; Persion *persion = [[Persion alloc] init];
#import "Persion.h" @implementation Person - (id)init { // allow superclass to initialize its state first if (self == [super init]) { age = 0; name = @"Bob"; // do other initialization... } return self; } @end Multiple init methods - (id)init; - (id)initWithName:(NSString *)name; - (id)initWithNameAndAge:(NSString *)name age:(int)age; Less specific ones typically call more specific with default values - (id)init { return [self initWithName:@"No Name"]; } - (id)initWithName:(NSString *)name { return [self initWithNameAndAge:name age:0]; } Persion *persion = [[Persion alloc] init]; // ... [persion release]; // Object is deallocated [persion doSomething]; // Crash! persion = nil; [persion doSomething]; // No effect Implementing a -dealloc method #import "Persion.h" @implementation Persion - (void)dealloc { // Do any cleanup that's necessary // ... [name release]; // when we're done, call super to clean us up [super dealloc]; } @end 构造函数和析构函数。 Object Ownership #import "Persion.h" @implementation Persion - (NSString *)name { return name; } // retain - (void)setName:(NSString *)newName { if (name != newName) { [name release]; name = [newName retain]; // name's retain count has been bumped up by 1 } } // copy - (void)setName:(NSString *)newName { if (name != newName) { [name release]; name = [newName retain]; // name's retain count has been bumped up by 1 } } Returning a newly created object - (NSString *)fullName { NSString *result; result = [[NSString alloc] initWithFormat:@"%@ %@", firstName, lastName]; [result autorelease]; return result; } Autorelease is not garbage collection, Objective-C on iPhone OS does not have garbage collection. C#的GC,优势尽显,这也表现出来的“高级”的地方吧! Defining Properites @property int age; @property (copy) NSString *name; @property (readonly) BOOL canLegallyVote; Synthesizing Properties - (int)age { return age; } - (void)setAge:(int)value { age = value; } - (NSString *)name { return name; } - (void)setName:(NSString *)value { if(value != name) { [name release]; name = [value copy]; } } @implementation Person @synthesize age; @synthesize name; - (BOOL)canLegallyVote { return (age > 17); } Memory namagement policies @property (assign) NSString *name; // pointer assignment @property (retain) NSString *name; // retain called @property (copy) NSString *name; // copy called Property Names vs. Instance Variables @interface Persion : NSObject { int numberOfYearsOld; } @property int age; @end @implemetation Persion @synthesize age = numberOfYearsOld; @end
http://v.163.com/special/opencourse/iphonekaifa.html http://rotator.youkulabs.com/?f5527199o1p0 |
请发表评论