ios - Storyboard名称 :bundle: is allocating memory for stoyboard object?

标签 ios memory memory-management uistoryboard allocation

假设

NSNumber *number =[[NSNumber alloc] numberWithInetger] 

编辑上面一行应该是:

NSNumber *number =[[NSNumber alloc] initWithInteger:0] 

这是为变量分配内存

但这不是:

NSNumber *number = [NSNumber numberWithInteger:1]

现在就我而言,我想调查这条线后面发生的事情:

 _storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
  1. 通过在循环中重复 (storyboardWithName) 方法,我每次都分配不同的内存?如果我的 Storyboard很大,这是不好的做法吗?
  2. 如果我在一个方法 block 中使用此方法 (storyboardWithName),是否最好只在该方法中定义它?完成方法 block 后会自动释放吗?如果定义为类成员变量呢?
  3. 如果我分配这个对象,它何时会被内存管理 (@autoreleasepool) 取消分配?*

基本上这对我来说意味着什么:

And for this method says:

  • storyboardWithName:bundle:

Creates and returns a storyboard object for the specified storyboard resource file.

.....

Return Value A storyboard object for the specified file. If no storyboard resource file matching name exists, an exception is thrown with description: Could not find a storyboard named 'XXXXXX' in bundle....

Discussion Use this method to retrieve the storyboard object containing the view controller graph you want to access. All of the resources associated with the storyboard must be in the bundle indicated by the storyboardBundleOrNil parameter.

is retaining object but since we did not alloc/copy there won't be need to release the variable.

https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIStoryboard_Class/#//apple_ref/doc/uid/TP40010909-CH1-SW6

更新

这是我从这张幻灯片中理解的,我有点困惑!

enter image description here

幻灯片链接:http://www.slideshare.net/whalec1/ios-memory-management-basis

最佳答案

你问题的第一部分是错误的。

你说

NSNumber *number = [NSNumber numberWithInteger:1]

不分配内存。当然可以。

该方法是一种“便捷方法”。它是一种在内部分配和初始化对象的类方法。

NSNumber 是一个特例。在内部,系统对常用数值进行一些特殊处理。例如,如果您创建多个 numberWithInteger:1 实例,您将获得指向同一对象的指针,而不是多个具有相同值的唯一对象。

关于 Storyboard, Storyboard是一个包含其他对象、进行文件管理等的大型复杂对象。当您使用 storyboardWithName 创建 Storyboard时,系统不会加载所有 View Controller Storyboard。它创建顶级对象,然后根据需要创建 View Controller 。

如果您在循环中重复调用 storyboardWithName 那么是的,它每次都会创建一个新对象。不要那样做。您应该在应用程序的生命周期中为每个名称创建一次 Storyboard对象,然后始终使用该 Storyboard的单个实例。

关于ios - Storyboard名称 :bundle: is allocating memory for stoyboard object?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37658062/

相关文章:

ios - 如何在核心数据中存储数组(Swift)

IOS 设备方向停留在纵向模式,没有横向

iphone - UIManagedDocument 和核心数据

C# 使用 byte[] 变量后清除系统内存

c++ - 等同于 Linux 中的 dwAllocationGranularity?

c - 有没有办法测试无效的内存位置?

ios - 单元测试 UITextField 方法

按顺序散列大量文件时出现 Python 内存错误

ios - 使用 block 的内存泄漏

c++ - 堆与堆栈内存使用 C++ 用于动态创建的类