与class名称相同的方法在Objective c中有什么作用?
what does a method of which name is same with the class name do in Objective c?
抱歉,我在https://developer.apple.com/library/ios/documentation/General/Conceptual/CocoaEncyclopedia/ClassClusters/ClassClusters.html看到了一段代码,我不明白Objective c中与class同名的方法有什么作用?
#import <foundation/foundation.h>
@interface MonthArray : NSArray
{
}
+ monthArray; // what is this function, a constructor?
- (unsigned)count;
- (id)objectAtIndex:(unsigned)index;
@end
定义一个与class同名的函数有什么特殊意义吗?构造函数?
与 class 同名的 class 方法通常是 工厂方法 。这是一个常见的约定,但语言并不要求或强制执行。
例如[NSArray array]
returns一个空数组。就像 [[NSArray alloc] init]
.
方法名称旁边的 +
使其成为 class 方法,这意味着您可以直接在 class 上调用它:[MonthArray monthArray]
.
这是一种约定,可让代码更具可读性和便利性。如果您想在自己的代码中构建此行为,您可以执行以下操作...
// DogClass.h
@interface DogClass : NSObject
+ (instancetype)dog;
- (instancetype)initWithDogName:(NSString *)name ownerName:(NSString *)ownerName birthYear:(NSNumber *)birthYear breed:(NSString *)breed;
@end
// DogClass.m
@implementation DogClass
+ (instancetype)dog {
DogClass *aDog = [[DogClass alloc] initWithDogName:@"" ownerName:@"" birthYear:@(0) breed:@""];
return aDog;
}
- (instancetype)initWithDogName:(NSString *)name ownerName:(NSString *)ownerName birthYear:(NSNumber *)birthYear breed:(NSString *)breed {
if (self = [super init]) {
self.name = name;
self.ownerName = ownerName;
self.birthYear = birthYear;
self.dateAdded = [NSDate date];
self.breed = breed;
}
return self;
}
@end
现在您可以用两种方式之一初始化 DogClass,因为它们以相同的方式初始化...
DogClass = [DogClass dog];
或
DogClass = [[DogClass alloc] initWithDogName:@"" ownerName:@"" birthYear:@(0) breed:@""]
抱歉,我在https://developer.apple.com/library/ios/documentation/General/Conceptual/CocoaEncyclopedia/ClassClusters/ClassClusters.html看到了一段代码,我不明白Objective c中与class同名的方法有什么作用?
#import <foundation/foundation.h>
@interface MonthArray : NSArray
{
}
+ monthArray; // what is this function, a constructor?
- (unsigned)count;
- (id)objectAtIndex:(unsigned)index;
@end
定义一个与class同名的函数有什么特殊意义吗?构造函数?
与 class 同名的 class 方法通常是 工厂方法 。这是一个常见的约定,但语言并不要求或强制执行。
例如[NSArray array]
returns一个空数组。就像 [[NSArray alloc] init]
.
方法名称旁边的 +
使其成为 class 方法,这意味着您可以直接在 class 上调用它:[MonthArray monthArray]
.
这是一种约定,可让代码更具可读性和便利性。如果您想在自己的代码中构建此行为,您可以执行以下操作...
// DogClass.h
@interface DogClass : NSObject
+ (instancetype)dog;
- (instancetype)initWithDogName:(NSString *)name ownerName:(NSString *)ownerName birthYear:(NSNumber *)birthYear breed:(NSString *)breed;
@end
// DogClass.m
@implementation DogClass
+ (instancetype)dog {
DogClass *aDog = [[DogClass alloc] initWithDogName:@"" ownerName:@"" birthYear:@(0) breed:@""];
return aDog;
}
- (instancetype)initWithDogName:(NSString *)name ownerName:(NSString *)ownerName birthYear:(NSNumber *)birthYear breed:(NSString *)breed {
if (self = [super init]) {
self.name = name;
self.ownerName = ownerName;
self.birthYear = birthYear;
self.dateAdded = [NSDate date];
self.breed = breed;
}
return self;
}
@end
现在您可以用两种方式之一初始化 DogClass,因为它们以相同的方式初始化...
DogClass = [DogClass dog];
或
DogClass = [[DogClass alloc] initWithDogName:@"" ownerName:@"" birthYear:@(0) breed:@""]