Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/111.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 在Swift中访问cellForRow(at:IndexPath)中的NSUserDefaults,内存使用率_Ios_Swift_Nsuserdefaults - Fatal编程技术网

Ios 在Swift中访问cellForRow(at:IndexPath)中的NSUserDefaults,内存使用率

Ios 在Swift中访问cellForRow(at:IndexPath)中的NSUserDefaults,内存使用率,ios,swift,nsuserdefaults,Ios,Swift,Nsuserdefaults,从cellForRow(at:indepath方法)中的NSUserDefaults检索数据是否会占用大量内存?代码如下所示: func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let recentFavorite = UserDefaults.standard.string("recentlyStoredFavorite") if sel

cellForRow(at:indepath
方法)中的
NSUserDefaults
检索数据是否会占用大量内存?代码如下所示:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  let recentFavorite = UserDefaults.standard.string("recentlyStoredFavorite")
  if self.favorite == recentFavorite {
    cell.imgForFavorite = self.imgForFavorite
  }
}
或者我应该保存
UserDefaults
中的值,并在
cellForRow
中使用该值吗

请帮助我理解这两种选择对记忆的影响。

苹果公司表示

在运行时,使用UserDefaults对象读取 您的应用程序从用户的默认数据库使用。UserDefaults缓存 避免每次打开用户默认数据库的信息 您需要一个默认值的时间

您应该“确定”在cellForRow中检索信息,因为它可能位于内存中的字典中(假设),由您提供的键获取,但是对于vadian的观点,您可以将其放在模型或属性中,并消除该假设

也可以考虑如果另一个进程改变了数据,如果需要观察用户缺省键,根据苹果E/P> 在运行时,使用UserDefaults对象读取 您的应用程序从用户的默认数据库使用。UserDefaults缓存 避免每次打开用户默认数据库的信息 您需要一个默认值的时间

您应该“确定”在cellForRow中检索信息,因为它可能位于内存中的字典中(假设),由您提供的键获取,但是对于vadian的观点,您可以将其放在模型或属性中,并消除该假设


此外,考虑数据是否会被另一个进程更改,如果需要观察USER Debug Orth.E/P>不要这样做。一个潜在的内存使用问题在性能问题上可以忽略不计,一次又一次地读取相同的值。一个非常有效的方法是在数据模型中添加一个属性<代码> iBase并在POPUL中设置它。设置数据源数组。@vadian但这是一个简单的字典查找。我真的怀疑这里是否存在任何相关的性能问题。但是,是的,将值存储在类属性中可能更好。不要这样做。与性能问题相比,反复读取同一值的潜在内存使用问题可以忽略不计。一种非常有效的方法是在数据模型中添加一个属性

isFavorite
,并在填充数据源数组时设置它。@vadian但这是一个简单的字典查找。我确实怀疑这里是否存在任何相关的性能问题。但是,是的,将值存储在类属性中可能会更好。