gpt4 book ai didi

ios - 在 iOS 中存储多个静态数据的最佳实践

转载 作者:行者123 更新时间:2023-12-03 16:52:40 25 4
gpt4 key购买 nike

我需要存储预先配置的值,这些值稍后将用于用户可以从 iOS 应用程序的表格中进行选择的选项,可能有大约 5-10 个选项。
就性能和效率而言,存储此类数据的最佳方式是什么?
我可能会想到几种方法,例如:

  • 在渲染方法中硬编码
  • 数组
  • plist 文件
  • 核心数据

谢谢

最佳答案

静态数据应存储在将加载值的适当类的静态变量中。

无论是否从文件加载字典,这里介绍如何静态加载字典或数组,以便在应用程序中只执行一次。

//.h
@interface MyApp

+(void) initialize; //will only be called once when the class is loaded

//.m

static NSArray *myListOfStuff;

@implementation MyApp

+(void) initialize {
//...either load your values from a file or hard code the values here
//init and assign values to myListOfStuff
}

//a statis getter for the list
+(NSArray *) listOfStuff {
return myListOfStuff;
}


//Client Code to get the list in your app
NSArray *myList = [MyApp listOfStuff];

//This memory will not be released for the life of the application.
//it will be loaded once and only once - its efficient

如果您想要持久化,Google 会将字典或数组持久化到 plist我向您展示的是如何在对象模型中有效地构建加载静态数据的结构无论持久性方法如何

关于ios - 在 iOS 中存储多个静态数据的最佳实践,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11823967/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com