leibie
申明
@interface NSString(NumberConvenience)
-(NSNumber *)lenthAsNumber;
@end
实现
@implementation NSString(NumberConvenience)
-(NSNumber *)lenthAsNumber{
unsigned int length = [self length];
return ([NSNumber numberWithUnsignedint : length]);
}
@end
正式协议
申明
@protocol myprotocol
-(id) someprotocol : (NSString *)zone;
@optional
-(void)method1;
@required
-(void)mthod2;
@end
采用
@interface myClass: NSOject<myprotocol, otherprotocol>
{
}
@end
特性
申明 @property(attributes) type name;
@interface myclass : someclass
{
int someproperty;
}
@property int someproperty;
@property float otherproperty;
@end
实现
@implementation myclass
@synthesize someproperty;
@synthesize property1 = otherproperty;
@end
object-c 学习小记
最新推荐文章于 2025-08-05 00:10:04 发布
本文介绍了 Objective-C 中的类扩展方法实现字符串长度获取及通过协议定义与实现类的方法。涉及类扩展的具体语法、协议的声明及如何在类中遵循这些协议。
2644

被折叠的 条评论
为什么被折叠?



