Object C 语法入门

https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/Introduction/Introduction.html

Class Defines
@interface SimpleClass : NSObject
{
NSString *_myNonPropertyInstanceVariable;
}
@end

Properties
@interface Person : NSObject

atomic原子,多线程保护
@property (readonly is readwrite getter=isFinished atomic nonatomic strong(默认) weak copy) NSString *firstName; (内部用_firstName存储)
@property NSString *lastName;

@end

@implement Person
{
NSString *_anotherCustomInstanceVariable;//不想公开
}
//没有下面这句,就默认是@synthesize propertyName = _propertyName
@synthesize propertyName = instanceVariableName;(指定存储值)

@end

init方法
- (id)init {
self = [super init];

if (self) {
    // initialize instance variables here
}

return self;

}

实例方法
- (XYZObject *)someImportantObject {
if (!_someImportantObject) {
_someImportantObject = [[XYZObject alloc] init];
}

return _someImportantObject;

}
类方法
+ (XYZObject *)someImportantObject {
if (!_someImportantObject) {
_someImportantObject = [[XYZObject alloc] init];
}

return _someImportantObject;

}

strong 修饰的属性会在赋值时调用被指向对象的 retain 方法,导致其引用计数加1 。weak 则不会。另外还有个 unsafe_unretained,跟 weak 类似,区别是被指向对象消失时不会“自动“变成 nil 。

NSString *someString = @"Hello, World!";

NSNumber *myBOOL = @YES;
NSNumber *myFloat = @3.14f;
NSNumber *myInt = @42;
NSNumber *myLong = @42L;

不需要强类型
运行时报错
id someObject = @”Hello, World!”;
[someObject removeAllObjects];
编译时报错
NSString *someObject = @”Hello, World!”;
[someObject removeAllObjects];

if ([firstPerson isEqual:secondPerson]) {
    // firstPerson is identical to secondPerson
}

if ([someDate compare:anotherDate] == NSOrderedAscending) {
    // someDate is earlier than anotherDate
}


XYZPerson *somePerson;(会自动=nil)
// somePerson is automatically set to nil

属性自动会synthesize,需要定制才需要显式synthesize

get方法同属性名
set方法在属性名前面加set大写
[object valueForKey:(NSString*)]可以通过属性名来访问属性值

属性对应的变量名前面加了下划线_

@synthesize firstName; 变量同属性名

//下面是默认实习,有没有都一样
@synthesize firstName = _firstName

-(id)init
{
self = [super init];
}

  • (void)dealloc {
    NSLog(@”XYZPerson is being deallocated”);
    }

//分类,主要用来实现私有。可以在.m中用category
@interface ClassName (CategoryName)

@end

@implementation XYZPerson (XYZPersonNameDisplayAdditions)
- (NSString *)lastNameFirstNameString {
return [NSString stringWithFormat:@”%@, %@”, self.lastName, self.firstName];
}
@end

Category
用于给class及其subclass添加新的方法
有自己单独的 .h 和 .m 文件
用于添加新方法,而不能添加新属性(property)

Extension
Extension常被称为是匿名的Category
用于给类添加新方法,但只作用于原始类,不作用于subclass
只能对有implementation源代码的类写Extension,对于没有implementation源代码的类,比如framework class,是不可以的
Extension可以给原始类添加新方法,以及新属性

@interface XYZPerson : NSObject

@property (readonly) NSString *uniqueIdentifier;
- (void)assignUniqueIdentifier;
@end

//下面隐藏信息,只有调用着才能调用、看到。需要源码实现。
@interface XYZPerson ()
@property (readwrite) NSString *uniqueIdentifier;
@end

@implementation XYZPerson

@end

Protocols有点像接口声明

@protocol XYZPieChartViewDataSource
@require
- (NSUInteger)numberOfSegments;
- (CGFloat)sizeOfSegmentAtIndex:(NSUInteger)segmentIndex;
@optional
- (NSString *)titleForSegmentAtIndex:(NSUInteger)segmentIndex;
@end

@protocol XYZPieChartViewDataSource
- (NSUInteger)numberOfSegments;
- (CGFloat)sizeOfSegmentAtIndex:(NSUInteger)segmentIndex;
@optional
- (NSString *)titleForSegmentAtIndex:(NSUInteger)segmentIndex;
@end

if ([self.dataSource respondsToSelector:@selector(titleForSegmentAtIndex:)]) {
    thisSegmentTitle = [self.dataSource titleForSegmentAtIndex:index];
}

//接口(协议)继承
@protocol MyProtocol

@end

//实现借口
@interface MyClass : NSObject

@end

@interface MyClass : NSObject

, NO 是 0 , BOOL 本身就是个 char 。 4、IBOutlet、IBAction是啥玩意,总能看到。 这两个东西其实在语法中没有太大的作用。如果你希望在Interface Builder中能看 ,NO是0,BOOL本身就是个char。 4、IBOutlet、IBAction是啥玩意,总能看到。 这两个东西其实在语法中没有太大的作用。如果你希望在Interface Builder中能看到这个控件对象,那么在定义的时候前面加上IBOutlet,在IB里就能看到这个对象的outlet,如果你希望在Interface Builder里控制某个对象执行某些动作,就在方法前面加上(IBAction)。 而这两个东西实际上和void是一样的。 5、nil。 Objective-C里的NULL(空)就这么写,表示空指针。 6、为什么是@”字符串”而不是”字符串” 前面加上@符号,编译器在编译的时候会在程序中给你留出位置,这样才能保证这个字符串不会丢失。反正记住,如果你要想把某些字符串写死在程序里,就要用@”字符串”,如果忘了用@,程序应该会出错。 superzhou大侠指正: 6、为什么是@”字符串”而不是”字符串” ”字符串”是C的字符串,@”"是把C的字符串转成NSString的一个简写. 在需要NSString的地方才需要这个转化,例如NSLog里面. 在需要C string的地方,还是用”字符串”的. 另外,@”"这个转换是不支持中文的.例如NSLog(@”字符串”); 是一定输出不了中文的. 四、Objective-C 2.0 Objective-C 2.0是Leopard新增加的一门语言,其实和原来的Objective-C是一样的。主要是增加了属性。详细的内容这里不写了,可以参阅Allen Dang的这篇文章,写的很明白。 http://blog.codingmylife.com/?p=81 五、总结 现在来总结一下怎么看Objective-C的代码和怎么开始学Objective-C吧。 1、记住Objective-C就是C,不是火星语,这个很关键。 2、记住你自己看不懂不表示脑子迟钝,大部分人第一次看Objective-C的代码可能比你还要迟钝。 3、把CocoaChina.com加入收藏夹,看不明白代码就来再看一遍这篇开宗明义的好文。 4、文档很关键,当你看不懂某些东西说的是什么的时候,先查Cocoachina,再看英文文档里面的API说明,尤其这个类是以NS开头的时候。再不行就去google搜,直接把你要查的方法贴进google,通常能找到不少人也在问同样的问题,自然也有热心人活雷锋帮助回答。 5、可以看hello world例子,但是不能总看,看多了真的会晕。另外,千万要放弃苹果官方的Currency Converter货币转换的例子,那个例子是毒药,刚学的时候越看越蒙。 6、学习一门语言最好的方法是先用,和学外语一样,当你会说的时候自然会读。给自己设立一个简单的目标,比如做一个简单的程序,然后一点点解决问题。这样学习起来比只看例子快得多。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值