__weak在使用自动引用计数(ARC)时,是否有iOS中的类列表无法使用指针引用?
Apple 过渡到ARC发行说明目前仅列出了Mac类:
哪些类不支持归零弱引用?
您当前无法创建对以下类的实例的归零弱引用:
NSATSTypesetter,NSColorSpace,NSFont,NSFontManager,NSFontPanel,NSImage,NSMenuView,NSParagraphStyle,NSSimpleHorizontalTypesetter,NSTableCellView,NSTextView,NSViewController,NSWindow,和NSWindowController.此外,在OS X中,AV Foundation框架中的任何类都不支持弱引用.
是否有类似的UIKit类列表,甚至是特定于iOS的类?
谢谢.
所以我发现ARC仅支持运行iOS 4.0+的设备.
我的应用程序(在商店中)如何在运行iOS 3.0的设备上工作(肯定使用ARC)?
我很困惑.
从下面的解决方案:只要我不使用归零弱引用,似乎使用ARC仍然适用于iOS 3.x,但Apple只是没有保证结果.
我有一个问题,我得到了错误的访问异常,但只有在运行测试版本时(在调试版本中调用相同的方法不会导致问题出现).该项目启用了ARC,我使用Xcode 4.3在iPad 5.1模拟器上运行它:
这就是问题出现的地方:
- (void)testChangeFoodNotification {
Player* p = [[Player alloc] init];
[p addObserver:self forKeyPath:@"food" options:0 context:0]; // <-EXC_BAD_ACCESS (code=2)
p.food += 1;
STAssertTrue(_wasNotifiedOfFoodChange, nil);
}
Run Code Online (Sandbox Code Playgroud)
在addObserver:调用该方法时,似乎不应该释放任何涉及的对象,那么可能导致异常的原因是什么?
编辑:
如果不清楚但上面的代码是作为测试用例的一部分执行(使用标准的Xcode OCUnit),请道歉.此外,如果它澄清了这里的任何内容,那么来自播放器类的相关代码(还有其他的ivars和方法,但它们与被测试的属性或方法没有任何关联):
// Public interface
@interface Player : NSObject
@property (nonatomic, assign) NSInteger food;
@end
// Private interface
@interface Player() {
NSInteger _food;
}
@end
@implementation Player
@synthesize food = _food;
#pragma mark - Getters/Setters
- (void)setFood:(NSInteger)food {
[self willChangeValueForKey:@"food"];
_food = food;
[self didChangeValueForKey:@"food"];
}
Run Code Online (Sandbox Code Playgroud) 我有一个dispatch_once用于创建静态对象的类方法.在dispatch_once我使用的块内,[self class]并想知道我是否需要使用弱引用self来避免保留周期?
+ (NSArray *)accountNames{
static NSArray *names = nil;
static dispatch_once_t predicate;
dispatch_once(&predicate, ^{
names = [[[self class] accounts] allKeys];
names = [names sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
});
return names;
}
Run Code Online (Sandbox Code Playgroud)
如果我使用弱引用,self我会收到警告:
+ (NSArray *)accountNames{
static NSArray *names = nil;
static dispatch_once_t predicate;
__weak TUAccount *wself = self;
dispatch_once(&predicate, ^{
names = [[[wself class] accounts] allKeys];
names = [names sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
});
return names;
}
Run Code Online (Sandbox Code Playgroud)
不兼容的指针类型使用类型为'const Class'的表达式初始化'TUAccount*__ weak'
因为我收到警告,我认为self在这种情况下我不需要使用弱引用,但我想看看你们的想法.
cocoa weak-references objective-c objective-c-blocks automatic-ref-counting
我遇到了一个让我完全难过的问题.我将用代码示例说明:
@interface Crasher ()
@property (nonatomic, strong) NSArray *array;
@end
@implementation Crasher
- (void)crash;
{
NSMutableArray *mutable = [NSMutableArray array];
NSArray *items = @[@0, @1, @2, @3];
if ([@YES boolValue])
{
[items enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
[mutable addObject:obj];
}];
}
else
{
[items enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
[mutable addObject:obj];
}];
}
[self setArray:mutable];
}
@end
Run Code Online (Sandbox Code Playgroud)
[self setArray:mutable]启用ARC并在设备上运行时,上面的代码在线路上崩溃.代码永远不会在模拟器上崩溃,也不会在禁用ARC的设备上崩溃.使用NSZombieEnabled表示setter正在尝试保留已经解除分配的数组.
如果第二个[mutable addObject:obj]调用被注释掉,它不会崩溃(但这个代码从未在第一个地方执行).
我上传了一个项目,向Github演示了这次崩溃aidansteele/arc-crash.我正在使用Xcode 4.5.2.它似乎不会出现在Xcode 4.6上,但仍然在开发人员预览中.我究竟做错了什么?
解决这个问题的答案(在问题上,以便我有更多的空间),我不相信问题在于,-[NSArray enumerateObjectsUsingBlock:] …
我有一个@class Foo包含__weak id barivar的.来自不同类中的方法的若干操作可能导致对象消失并因此被bar弄脏.
我希望在自动将ARC标记为ivar时执行操作.
如果可能的话,我希望避免bar变成属性或使用键值观察.
这甚至可能吗?如果没有,KVO可以用于非财产ivars吗?
我正在尝试将我的iOs项目转换为使用ARC,但是下面收到一个lipo错误是命令和错误
命令:
CreateUniversalBinary /Users/samichaudry/Library/Developer/Xcode/DerivedData/CartoonQuiz-bqmbxfcdkldmbqawgrfmjtebypuk/Build/Products/Debug-iphoneos/CartoonQuiz.app/CartoonQuiz normal "armv7 armv7s"
cd "/Users/samichaudry/Projetcs/CartoonQuiz/Application/V1.01 New Design Universal/Application"
setenv PATH "/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin:/Applications/Xcode.app/Contents/Developer/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin"
lipo -create /Users/samichaudry/Library/Developer/Xcode/DerivedData/CartoonQuiz-bqmbxfcdkldmbqawgrfmjtebypuk/Build/Intermediates/CartoonQuiz.build/Debug-iphoneos/CartoonQuiz.build/Objects-normal/armv7/CartoonQuiz /Users/samichaudry/Library/Developer/Xcode/DerivedData/CartoonQuiz-bqmbxfcdkldmbqawgrfmjtebypuk/Build/Intermediates/CartoonQuiz.build/Debug-iphoneos/CartoonQuiz.build/Objects-normal/armv7s/CartoonQuiz -output /Users/samichaudry/Library/Developer/Xcode/DerivedData/CartoonQuiz-bqmbxfcdkldmbqawgrfmjtebypuk/Build/Products/Debug-iphoneos/CartoonQuiz.app/CartoonQuiz
Run Code Online (Sandbox Code Playgroud)
错误:
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/lipo: can't figure out the architecture type of: /Users/samichaudry/Library/Developer/Xcode/DerivedData/CartoonQuiz-bqmbxfcdkldmbqawgrfmjtebypuk/Build/Intermediates/CartoonQuiz.build/Debug-iphoneos/CartoonQuiz.build/Objects-normal/armv7/CartoonQuizCommand /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/lipo failed with exit code 1
Run Code Online (Sandbox Code Playgroud) 我在使用带有UITableView的UISearchDisplayController时有一个僵尸.
UISearchDisplayController(以及视图的其余部分)是通过界面构建器设置的(xcode 5上的故事板,仅使用ARC和iOS 7).
searchDisplayController由这两种方法使用
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString {
[self filterContentForSearchText:searchString scope:
[[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:[self.searchDisplayController.searchBar selectedScopeButtonIndex]]];
return YES;
}
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchScope:(NSInteger)searchOption {
[self filterContentForSearchText:self.searchDisplayController.searchBar.text scope:
[[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:searchOption]];
return YES;
}
Run Code Online (Sandbox Code Playgroud)
仪器给了我这个信息
# Event Type ? RefCt RefCt Timestamp Responsible Library Responsible Caller
0 Malloc +1 1 00:10.739.188 UIKit -[UITableView setTableHeaderBackgroundColor:]
1 Retain +1 2 00:10.739.214 UIKit -[UIView(Internal) _addSubview:positioned:relativeTo:]
2 Retain +1 3 00:10.739.234 UIKit -[UISearchDisplayController _configureSearchBarForTableView]
3 Retain +1 4 00:10.739.291 UIKit -[UIView(Hierarchy) subviews]
4 Retain +1 …Run Code Online (Sandbox Code Playgroud) storyboard nszombie uisearchdisplaycontroller automatic-ref-counting
将JSONkit用于启用了ARC的项目,并且存在问题.我正确地标记了文件-fno-objc-arc并尝试了构建,但它仍然抛出了启用ARC的错误.我有什么想法可以错过吗?
为清楚起见,我在Build Phases-> Compile Sources-> JSONkit.m中标记了该文件
编辑:错误的屏幕截图.这些是我在为ARC忽略的文件添加标志之前得到的相同错误

我正在参加关于编译器的大学课程,我们刚刚讨论了垃圾收集和释放内存的方法.然而,在课堂讲座和我们的教科书中,我被引导相信引用计数不是管理记忆的好方法.
原因是引用计数非常昂贵,因为程序必须插入许多附加指令来递增和递减引用计数.此外,每次引用计数更改时,程序必须检查它是否等于零,如果是,则回收内存.
我的教科书甚至还有一句话:"总的来说,引用计数的问题超出了它的优点,很少用于编程语言环境中的自动存储管理.
我的问题是:这些合法性问题是否存在?Objective-c是否以某种方式避免它们?如果是这样的话?
garbage-collection reference-counting objective-c ios automatic-ref-counting
objective-c ×7
ios ×6
architecture ×1
cocoa ×1
cocoa-touch ×1
iphone ×1
ivar ×1
jsonkit ×1
lipo ×1
nszombie ×1
ocunit ×1
storyboard ×1
uikit ×1
xcode ×1
xcode4.6 ×1