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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值