我有一个具有以下属性的类:Objective-C的命名约定方法
- 名称
- 电子邮件
- 用户名
我想这些属性转换为字典。我怎样才能正确地说出遵循苹果惯例的方法?
我在想:
- (NSDictionary *) convertToDictionary {}
我有一个具有以下属性的类:Objective-C的命名约定方法
我想这些属性转换为字典。我怎样才能正确地说出遵循苹果惯例的方法?
我在想:
- (NSDictionary *) convertToDictionary {}
如何只-dictionaryRepresentation
,传达你可以得到存储在字典的形式对象中的数据的概念?所以,你会说:
NSDictionary *personDict = [person dictionaryRepresentation];
另一种合理的选择是-properties
,离开返回类型出了名的:
NSDictionary *personDict = [person properties];
认为,这从一个NSString
到NSData
你去:
NSData* data = [str dataUsingEncoding:NSUTF8StringEncoding];
同样,你可以只是做:
- (NSDictionary *)dictionary;
因为您没有参数。或者考虑从NSData
去NSString
你做:
[[[NSString alloc] initWithData:data encoding:encoding] autorelease]
类方法,如果有一个,看起来像:
[NSString stringWithData:data encoding:encoding]
,所以你可以把它的类别上NSDictionary
。假设你的类被称为Account
:
+ (NSDictionary *)dictionaryWithAccount:(Account *)account;
取决于你想看起来更好:
Account *account = ...;
[account dictionary];
[NSDictionary dictionaryWithAccount:account];
他有一个班级和一个实例。他为什么要把它变成一个类方法(+ dictionaryWithAccount :)?这没有什么意义。 –
@ChristianKienle:你是对的,编辑过。 – Claudiu
什么叫类? – Claudiu
另请参阅Apple的[Cocoa编码指南:命名方法](https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/CodingGuidelines/Articles/NamingMethods.html)。 – Rob