- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
为什么它在调试/发布/模拟器/设备组合之间会有所不同? (下面是示例代码。)
我继承了一些在模拟器和调试设备上“工作”的代码,但在发布设备上不工作(用于上一个 LLVM)。它与 ARC 和一个弱属性(应该很强)有关,但我不完全理解发生了什么,希望有人可以向我解释它以及为什么它在调试/发布/设备/模拟器之间有所不同。
我创建了一个简单的示例项目来演示这一点,其中包含 AppDelegate.m 中的所有代码,如下所示。在模拟器中运行时,记录的输出为:
-[BaseThingMaker baseMakeThing]: returning a Thing
-[ThingMaker makeThing]: returning a Thing
-[AppDelegate application:didFinishLaunchingWithOptions:]: got a Thing
在设备上运行时,记录的输出为:
-[BaseThingMaker baseMakeThing]: returning a Thing
-[ThingMaker makeThing]: returning a (null)
-[AppDelegate application:didFinishLaunchingWithOptions:]: got a (null)
在设备上(处于“调试”或“发布”状态),ThingMaker
中的弱 self.thing
为 null,即使对于方法完成之前的日志消息也是如此。在模拟器上,返回 self.thing
对象。
单窗口应用程序 (AppDelegate.m) 的示例代码:
#import "AppDelegate.h"
@interface Thing : NSObject
@end
@implementation Thing
@end
@interface BaseThingMaker : NSObject
-(Thing *)baseMakeThing;
@end
@implementation BaseThingMaker
-(Thing *)baseMakeThing
{
Thing *thing = [Thing new];
NSLog(@"%s: returning a %@", __PRETTY_FUNCTION__, NSStringFromClass([thing class]));
return thing;
}
@end
@interface ThingMaker : NSObject
- (id)makeThing;
@end
@interface ThingMaker ()
@property (weak, nonatomic) id thing;
@end
@implementation ThingMaker
- (id)makeThing
{
BaseThingMaker *baseThingMaker = [BaseThingMaker new];
self.thing = [baseThingMaker baseMakeThing];
NSLog(@"%s: returning a %@", __PRETTY_FUNCTION__, NSStringFromClass([self.thing class]));
return self.thing;
}
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
ThingMaker *thingMaker = [ThingMaker new];
id thingIWant = [thingMaker makeThing];
NSLog(@"%s: got a %@", __PRETTY_FUNCTION__, NSStringFromClass([thingIWant class]));
return YES;
}
@end
最佳答案
方法baseMakeThing
不会返回其调用所拥有的对象,在本例中它返回一个自动释放的对象。如果它是一个自动释放对象,它将有一个所有者 - 自动释放池 - 直到运行循环循环时该池清空。因此,在发生这种情况之前,对弱属性的分配将一直有效,并且代码将看起来可以工作...
除非编译器进行优化,否则编译器有时会将对象从自动释放池中拉出并以这种方式获取所有权,而不是保留自动释放的对象。如果编译器这样做,那么在分配给弱属性后,编译器可以立即放弃所有权,因为稍后在 makeThing
中不会使用该引用,这又将使弱属性为空...
您看到的差异取决于编译器根据构建类型进行的优化程度。
关于cocoa-touch - 为什么实例方法中的弱成员可以在方法完成之前被清空?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23960634/
我有一个 QVariantList。我怎样才能清空它。 我试过了 myList.clear() 和 myList.empty(); 但运气不好。有什么想法吗? 最佳答案 A QVariantList只
我在创建一个空的 JTabbedPane 时遇到问题,其中在 GUI 上唯一可见的部分是选项卡行。 每次我添加一个带有“空”组件的新选项卡时,JTabbedPane 的高度都会增加,但这是为什么呢?
我有一个简单的 Windows 窗体,其中包含一个名为 list 的 ListView 对象。在窗体上,一个按钮使我能够在单击 list.Items.Clear() 时清空列表。这很好用。 现在我有一
最近遇到一点麻烦事,新安装的PHPwind6.0正式版社区在导入之前的会员帐号资料时,发现很多会员的mail地址貌似胡乱填写的,之前的PHPwind5.5版本没有开启mail地址验证功能,所以估计很
我在 Visual Basic for Applications 中遇到一个 Collection 对象问题(我在 Excel 中使用它) 我有这段代码试图清空一个我必须重新使用的 Collectio
我想清空 CKEditor 中的文本区域。 我可以使用 SetData(' ') 清除 TextArea,但只能在页面加载后清除一次。 我只在 Onchange 事件中编写了 jquery 函数。
使用 iOS6 的很棒的新 UICollectionView 我如何能够删除一个大循环中的所有 UICollectionViewCell 对象? 假设我已经将所有数据加载到其中,我点击刷新,我想删除那
有没有办法在不诉诸目标的情况下删除 ItemGroup 的内容?我正在寻找相当于: 谢谢 最佳答案 不可以,正如文档所述,Remove 只能包含在 Target 内的 ItemGroup 中
我想清除数组对象中的所有元素(可以是标准的 PHP 数组、ArrayObject 或基本上实现基本数组接口(interface)的任何其他对象,如 Itertable、ArrayAccess、Coun
我有一个 9x9 的网格布局,并生成 Jtextareas 来填充它。如果用户按下按钮,我希望网格布局再次变空,以便我可以再次填充它,但与之前填充的内容无关。是否有某种命令,例如 gridlayout
使用 iOS6 的很棒的新 UICollectionView 我如何能够删除一个大循环中的所有 UICollectionViewCell 对象? 假设我已经将所有数据加载到其中,我点击刷新,我想删除那
我正在尝试在 Eclipse 中使用 Nutch 运行爬网。 我正在使用一个名为 urls 的文件,它包含 http://www.google.com/ 但是,当我运行该项目时,Generator 类
$(document).ready(function () { $('#btnSearch').bind('click', function () {
我有一个 jquery 函数,它在单击事件时清空一个 div,获取一些 json,然后附加到已清除的 div。但是,我试图附加到所述 div 的复选框没有出现。这是我的代码: $(function()
我见过类似的问题,但没有一个和我的完全一样,而且我无法理解答案,所以我希望你能帮助我。 thread我指的是以某种方式解决了异步任务或其他问题,但我不确定那是什么。我的问题是我的布局有一个 TextV
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
一些事实: - 我的应用程序是客户端。 - 我有一个Socket 池。 - 多个 Thread 使用此池。 - 每个线程都可以超时。 - 超时时,Socket 返回到池中,即使没有从服务器读取回复。
请考虑以下代码,包括两个线程 buffering_thread(用一条消息填充缓冲区指针)和 sending_thread(清空缓冲区): #include "msg.cpp" msg * buffe
我制作了这个板,以随机顺序附加了一些框。 当单击按钮时,我想以新的随机顺序附加框,但显然不起作用(.board 保持为空) http://jsbin.com/pedanobawe/2/edit?htm
我有一个脚本,它将文件列表存储在一个数组中,如下所示 set -A my_array $(ls -tr $INPUT_DIRECTORY/*) 我必须清空这个 my_array 变量才能将其用于其他目
我是一名优秀的程序员,十分优秀!