NSDictionary 与自定义对象的性能比较
我正在使用 NSURLConnection 从服务器检索由我编写的某些 php 生成的 pList。我的 Plist 在根级别包含多个数组。这些数组包含服务器 NSDictionyObjects。检索 pList 后,我使用 NSPropertyLitSerialization 将数据序列化到 NSDictionary 中。
现在,一旦我有了这个 NSDictionary,我就会将值加载到我使用以下代码创建的自定义对象中。
NSMutableDictionary *newDict = [[NSMutableDictionary alloc]init];
//Create object array.
for (NSString *key in dict) {
NSArray *tempArr = [dict objectForKey:key];
NSMutableArray *gameObjectArray = [[NSMutableArray alloc]
initWithCapacity:tempArr.count];
for (NSDictionary *tempDict in tempArr) {
//Build Objects and add to array
GameObject *tempGame = [[GameObject alloc] initWithArr:tempDict];
//initWithArr initializes the values in the object using tempDict
[gameObjectArray addObject:tempGame];
[tempGame release];
}
[newDict setObject:gameObjectArray forKey:key];
[gameObjectArray release];
}
self.gameObjectsDict = newDict;
从现在开始,我在整个应用程序中使用存储 gameObjectsDict 的值对象。适用于各种内容,包括渲染 UITableView 并将数据传递到其他 viewControllers/视图。
我的问题: 将 pList 中的所有这些值放入值对象是否值得?
之前,我只是使用 NSDictionary 在应用程序中传递我的值,而且效果很好。我觉得使用值对象会更容易编码,也是更好的实践,但现在我使用值对象,感觉像是浪费了更多内存。感觉就像我正在传递这些充满数据的重物,而以前我只是在需要时从字典中获取值。
I'm retrieving a pList, generated by some php i wrote, from a server using NSURLConnection. My Plist contains several arrays at the root level. These array contain sever NSDictionyObjects. Once I retrieve the pList I use NSPropertyLitSerialization to serialize the data into a NSDictionary.
Now, once I have this NSDictionary I load the values into to custom objects I have created with the following code.
NSMutableDictionary *newDict = [[NSMutableDictionary alloc]init];
//Create object array.
for (NSString *key in dict) {
NSArray *tempArr = [dict objectForKey:key];
NSMutableArray *gameObjectArray = [[NSMutableArray alloc]
initWithCapacity:tempArr.count];
for (NSDictionary *tempDict in tempArr) {
//Build Objects and add to array
GameObject *tempGame = [[GameObject alloc] initWithArr:tempDict];
//initWithArr initializes the values in the object using tempDict
[gameObjectArray addObject:tempGame];
[tempGame release];
}
[newDict setObject:gameObjectArray forKey:key];
[gameObjectArray release];
}
self.gameObjectsDict = newDict;
From this point forward I use the value objects stored gameObjectsDict throughout my application. For all kinds of stuff including rendering UITableViews and passing data to other viewControllers/views.
My Question:
Is it worth it to put all these values from my pList into value Objects?
Before, I was just using the NSDictionary to pass my values around the app and it worked fine. I felt it will be easier to code and a better practice to use the value objects but now that Im using the value objects if feels like I'm wasting more memory. It feels like I'm passing these heavy objects full of data around where as before i was just grabbing a values from my dictionary when needed.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用自定义对象的好处之一是类型安全。当您在很多地方使用 objectForKey 时,可能很难记住哪种类型的对象具有哪些键。让编译器记住这一点很有帮助。
One benefit to using custom objects would be type safety. When you are using objectForKey in lot's of places it may be hard to remember which type of object has which keys. Having the compiler remember this for you is helpful.
如果您要将字典视为对象,则更好的做法是将值添加到自定义对象。它确实不会像您想象的那样消耗那么多内存。此外,它还会增强代码的可读性,这始终是一个优点。
If you're going to be treating the dictionaries as objectes, it is better practice to add the values to a custom object. It really doesn't use up as much memory as you would think. Also, it will enhance the readability of your code, which is always a plus.