I have a bunch of property data listed like this, but I am looking to write it much neater. What way would you recommend to write this neater? I have a lot more property data than these.
我列出了很多这样的属性数据,但我想写得更整洁一些。你建议用什么方法写这篇文章?我有更多的财产数据。
@property (strong,nonatomic) NSString *energyEnhancer;
@property (strong,nonatomic) NSString *energyEnhancer1;
@property (strong,nonatomic) NSString *energyEnhancer2;
@property (strong,nonatomic) NSString *energyEnhancer3;
I have to have property because I am passing data between view controllers.
我必须有属性,因为我在视图控制器之间传递数据。
4 个解决方案
#1
3
The short answer:
简短的回答:
The iOS Developer Collections Reference.
iOS开发人员集合引用。
The Whole Story:
整个故事:
I think it would be a good idea to take a broader look at some of the Foundation classes that deal with collections, such as NSArray
, NSSet
, NSDictionary and if you're feeling particularly esoteric, CFBag
.
我认为,最好更广泛地研究一些处理集合的基础类,比如NSArray, NSSet, NSDictionary,如果你觉得特别深奥的话,CFBag。
Your question suggests that you could benefit from reading up on general Cocoa patterns. Indeed, you can pass just about anything from one object to another. An NSArray
instance is certainly no different than several NSString
instances.
您的问题表明,您可以从阅读一般的Cocoa模式中获益。实际上,您可以将任何东西从一个对象传递到另一个对象。NSArray实例肯定与几个NSString实例没有什么不同。
For example, imagine we have an app called "PassingData" (GitHub link). I'm going to define a class which has our data, in this case, several "energyEnhancer" strings.
例如,假设我们有一个名为“PassingData”(GitHub链接)的应用程序。我将定义一个类,它有我们的数据,在这个例子中,有几个“energyEnhancer”字符串。
@interface PDDataSource : NSObject
@property (nonatomic, strong) NSString *engergyEnhancer;
@property (nonatomic, strong) NSString *engergyEnhancer2;
@property (nonatomic, strong) NSString *engergyEnhancer3;
@end
Then in our view controller, we may try to access the energy enhancers, like so:
然后在我们的视图控制器中,我们可以尝试访问能量增强器,如下所示:
- (void)logDataSourceWithStrings {
NSLog(@"Energy enhancer 1: %@", self.dataSource.energyEnhancer);
NSLog(@"Energy enhancer 2: %@", self.dataSource.energyEnhancer2);
NSLog(@"Energy enhancer 3: %@", self.dataSource.energyEnhancer3);
}
Another way to do this is like so:
另一种方法是:
- (void)logDataSourceWithArray {
for(NSInteger i = 0; i < self.dataSource.enhancers.count; i++) {
NSLog(@"Enhancer %i: %@", i, self.dataSource.enhancers[i]);
}
}
An added benefit to using an array is that you're no longer limited by the number of variables that you've declared at compile time. Your game or fitness app just got that much more robust.
使用数组的另一个好处是,您不再受编译时声明的变量数量的限制。你的游戏或健身应用程序变得更加健壮了。
This is only one way of accessing data that's in another object. Other strong contenders are delegate protocols, notifications, and callback blocks. Typically, if you're directly accessing data in another class, you're probably doing one of three things:
这只是访问另一个对象中的数据的一种方式。其他强大的竞争者包括委托协议、通知和回调块。通常,如果您直接访问另一个类中的数据,您可能要做以下三件事中的一件:
- Compositing: Creating a class that contains several objects that exist to help the parent class.
- 组合:创建一个包含多个对象的类,以帮助父类。
- Accessing a singleton. Singleton classes are universally accessible classes that can only be instantiated once. They're controversial, but there are appropriate use cases.
- 访问单例。单例类是普遍可访问的类,只能实例化一次。它们是有争议的,但是有适当的用例。
- Storing temporary state in an object.
- 在对象中存储临时状态。
If you want to model more than one kind of data, consider nesting your values (be it arrays, strings, numbers, or whatever) in a dictionary. This isn't always the case, though. I wouldn't want all of my classes to have a single NSDictionary
property. Use your best judgement.
如果您想建模不止一种数据,请考虑将值(无论是数组、字符串、数字还是其他)嵌套到字典中。但情况并非总是如此。我不想让所有的类都有一个NSDictionary属性。用你最好的判断。
Another good strategy when modeling is to use the plist editor in Xcode to mock an object. Then you can make a class (or classes) that match the plist, in code.
建模时的另一个好策略是使用Xcode中的plist编辑器来模拟对象。然后,您可以在代码中创建一个与plist匹配的类(或类)。
It's really worth your time to familiarize yourself with the conventions and Cocoa Design Patterns. Lotsa luck!
您确实值得花时间熟悉惯例和Cocoa设计模式。许多的运气!
#2
2
Rather than use an NSArray as some of the answers have suggested, I think you would be better off passing a Dictionary. This is more robust than an array, as you can use the keys of the dictionary to make sure you are getting the right values rather than depending on the order in an array.
与其使用NSArray的一些答案,我认为你最好通过字典。这比数组更健壮,因为您可以使用字典的键来确保您得到了正确的值,而不是依赖于数组中的顺序。
#3
1
You can write like this..
你可以这样写。
@property (strong,nonatomic) NSString *energyEnhancer,*energyEnhancer1,*energyEnhancer2,*energyEnhancer3;
#4
0
@property (strong, nonatomic) NSMutableArray *energyEnhancers;
would be better.
可能会更好。
#1
3
The short answer:
简短的回答:
The iOS Developer Collections Reference.
iOS开发人员集合引用。
The Whole Story:
整个故事:
I think it would be a good idea to take a broader look at some of the Foundation classes that deal with collections, such as NSArray
, NSSet
, NSDictionary and if you're feeling particularly esoteric, CFBag
.
我认为,最好更广泛地研究一些处理集合的基础类,比如NSArray, NSSet, NSDictionary,如果你觉得特别深奥的话,CFBag。
Your question suggests that you could benefit from reading up on general Cocoa patterns. Indeed, you can pass just about anything from one object to another. An NSArray
instance is certainly no different than several NSString
instances.
您的问题表明,您可以从阅读一般的Cocoa模式中获益。实际上,您可以将任何东西从一个对象传递到另一个对象。NSArray实例肯定与几个NSString实例没有什么不同。
For example, imagine we have an app called "PassingData" (GitHub link). I'm going to define a class which has our data, in this case, several "energyEnhancer" strings.
例如,假设我们有一个名为“PassingData”(GitHub链接)的应用程序。我将定义一个类,它有我们的数据,在这个例子中,有几个“energyEnhancer”字符串。
@interface PDDataSource : NSObject
@property (nonatomic, strong) NSString *engergyEnhancer;
@property (nonatomic, strong) NSString *engergyEnhancer2;
@property (nonatomic, strong) NSString *engergyEnhancer3;
@end
Then in our view controller, we may try to access the energy enhancers, like so:
然后在我们的视图控制器中,我们可以尝试访问能量增强器,如下所示:
- (void)logDataSourceWithStrings {
NSLog(@"Energy enhancer 1: %@", self.dataSource.energyEnhancer);
NSLog(@"Energy enhancer 2: %@", self.dataSource.energyEnhancer2);
NSLog(@"Energy enhancer 3: %@", self.dataSource.energyEnhancer3);
}
Another way to do this is like so:
另一种方法是:
- (void)logDataSourceWithArray {
for(NSInteger i = 0; i < self.dataSource.enhancers.count; i++) {
NSLog(@"Enhancer %i: %@", i, self.dataSource.enhancers[i]);
}
}
An added benefit to using an array is that you're no longer limited by the number of variables that you've declared at compile time. Your game or fitness app just got that much more robust.
使用数组的另一个好处是,您不再受编译时声明的变量数量的限制。你的游戏或健身应用程序变得更加健壮了。
This is only one way of accessing data that's in another object. Other strong contenders are delegate protocols, notifications, and callback blocks. Typically, if you're directly accessing data in another class, you're probably doing one of three things:
这只是访问另一个对象中的数据的一种方式。其他强大的竞争者包括委托协议、通知和回调块。通常,如果您直接访问另一个类中的数据,您可能要做以下三件事中的一件:
- Compositing: Creating a class that contains several objects that exist to help the parent class.
- 组合:创建一个包含多个对象的类,以帮助父类。
- Accessing a singleton. Singleton classes are universally accessible classes that can only be instantiated once. They're controversial, but there are appropriate use cases.
- 访问单例。单例类是普遍可访问的类,只能实例化一次。它们是有争议的,但是有适当的用例。
- Storing temporary state in an object.
- 在对象中存储临时状态。
If you want to model more than one kind of data, consider nesting your values (be it arrays, strings, numbers, or whatever) in a dictionary. This isn't always the case, though. I wouldn't want all of my classes to have a single NSDictionary
property. Use your best judgement.
如果您想建模不止一种数据,请考虑将值(无论是数组、字符串、数字还是其他)嵌套到字典中。但情况并非总是如此。我不想让所有的类都有一个NSDictionary属性。用你最好的判断。
Another good strategy when modeling is to use the plist editor in Xcode to mock an object. Then you can make a class (or classes) that match the plist, in code.
建模时的另一个好策略是使用Xcode中的plist编辑器来模拟对象。然后,您可以在代码中创建一个与plist匹配的类(或类)。
It's really worth your time to familiarize yourself with the conventions and Cocoa Design Patterns. Lotsa luck!
您确实值得花时间熟悉惯例和Cocoa设计模式。许多的运气!
#2
2
Rather than use an NSArray as some of the answers have suggested, I think you would be better off passing a Dictionary. This is more robust than an array, as you can use the keys of the dictionary to make sure you are getting the right values rather than depending on the order in an array.
与其使用NSArray的一些答案,我认为你最好通过字典。这比数组更健壮,因为您可以使用字典的键来确保您得到了正确的值,而不是依赖于数组中的顺序。
#3
1
You can write like this..
你可以这样写。
@property (strong,nonatomic) NSString *energyEnhancer,*energyEnhancer1,*energyEnhancer2,*energyEnhancer3;
#4
0
@property (strong, nonatomic) NSMutableArray *energyEnhancers;
would be better.
可能会更好。