automatic-ref-counting

Defining a property in iOS class extension

此生再无相见时 提交于 2019-11-29 09:26:28
I would like to add a property to UITableView in a Class Extension: @interface UITableViewController () @property NSString *entityString; @end Then I import the extension and then I use entityString property in a subclass of UITableViewController: @implementation CustomerTableViewController - (void)viewDidLoad { self.entityString = @"Customer"; ... [super viewDidLoad]; } ... Apple documentation says: the compiler will automatically synthesize the relevant accessor methods (...) inside the primary class implementation. But when I try to execute it I get this error: -[CustomerTableViewController

In Objective-C with ARC, is it true that we usually only need to specify nonatomic as property attributes?

廉价感情. 提交于 2019-11-29 09:23:26
问题 It is strange that in Big Nerd Ranch iOS 5 book (p.73) and Programming iOS 5 book (O'Reilly, p.314) (updadte: even Kochan's Objective-C book Fourth edition) , in the context of ARC, they say the default for properties attribute is assign ... But Apple's documentation says the default is strong . I also tried a simple program where if I don't specify strong , the program works ok, and if I specify strong , it works the same, and when assign is used instead, the compiler shows a warning, so it

ARC error when compiling

只愿长相守 提交于 2019-11-29 09:16:11
问题 I'm trying to compile using the LLVM GCC 4.0 compiler, and I get this error in multiple of my .m files: ARC forbids explicit message send of 'release' I've tried using -fno-objc-arc as a compiler flag but that returns the error: Unrecognized command line option "-fno-objc-arc". How can I solve this? 回答1: Simply remove all calls to -release . You're not allowed to call -release under ARC because the compiler will insert all the necessary retain / release calls for you. Read more about ARC here

What is the correct way to create a custom setter for a weak property in Objective C?

白昼怎懂夜的黑 提交于 2019-11-29 09:14:18
If I declare a property like this: @property(nonatomic,weak) Foo *someProperty; and I then declare a custom setter like so: - (void)setSomeProperty:(Foo *)someProp { _someProperty = someProp; //...more custom stuff } is there anything wrong with this? That is, the compiler should automatically synthesize the _someProperty ivar with the __weak modifier, so just doing the assignment in the setter above should suffice, correct? Yes, that's all there is to it. Of course you can specify a custom iVar if you'd like something other than _someProperty like: @synthesize someProperty = someProperty_; 来源

What does the “strong” keyword do

扶醉桌前 提交于 2019-11-29 09:12:14
I downloaded the Xcode 4.2 developer preview version and I created a cocoa application. But I found a very weird syntax in the delegate class: @property (strong) IBOutlet NSWindow *window; What does this mean? And the compiler can't even compile it. Thanks in advance! It indicates that this property is a strong relationship—an ownership. It's ARC 's version of the retain keyword in the same context. And the compiler can't even compile it. It's valid ARC code, so if your tools support ARC, they certainly should be able to compile it. Make sure that you're using Xcode 4.2 or later, and that you

Storing email in keychain impossible (KeychainItemWrapper)

非 Y 不嫁゛ 提交于 2019-11-29 09:11:11
I'm using the ARCified version of KeychainItemWrapper available at github , and I can't get it to store both email and password. KeychainItemWrapper *keychainItem = [[KeychainItemWrapper alloc] initWithIdentifier:@"myApp" accessGroup:@"MY_APP.com.yourcompany.GenericKeychainSuite"]; [keychainItem setObject:[self.email dataUsingEncoding:NSUTF8StringEncoding] forKey:(__bridge id)kSecAttrAccount]; [keychainItem setObject:self.password forKey:(__bridge id)kSecValueData]; I works perfectly as long as I store an email... that don't have an at sign (@) it. Otherwise, I get the error *** Assertion

bridged cast: __bridge_transfer vs __bridge with synthesized setter

南笙酒味 提交于 2019-11-29 08:20:39
问题 I am using ARC, and have confusion while using __bridge_transfer . I have a property userName as following: @property (nonatomic, retain) NSString *userName; ... @synthesize userName = _userName; ... CASE 1 : NSString *name = (__bridge_transfer NSString *)ABRecordCopyCompositeName(person); self.userName = name; CASE 2 : self.userName = (__bridge_transfer NSString *)ABRecordCopyCompositeName(person); where person is of type ABRecordRef . In CASE 1 , ARC would release local variable name (as

Setting NSError within a block, using ARC

眉间皱痕 提交于 2019-11-29 08:16:01
问题 I wish to set an NSError pointer from within a block in a project using automatic reference counting. What follows is a simplified version of my code: - (BOOL)frobnicateReturningError:(NSError **)error { NSArray *items = [NSArray arrayWithObjects:@"One", @"Two", @"Three", nil]; __block Frobnicator *blockSelf = self; [items enumerateObjectsUsingBlock:^(id item, NSUInteger idx, BOOL *stop) { [blockSelf doSomethingWithItem:item error:error]; }]; } This compiles but given error may be modified by

Retained Core Foundation Property

落爺英雄遲暮 提交于 2019-11-29 07:48:45
问题 (Xcode 4.2, iOS 5, ARC ) I have some properties of Core Foundation (/Graphics) objects that should take ownership of their objects. Now in these Apple docs I found this: In OS X v10.6 and later, you can use the __attribute__ keyword to specify that a Core Foundation property should be treated like an Objective-C object for memory management: @property(retain) __attribute__((NSObject)) CFDictionaryRef myDictionary; Unfortunately I couldn't find any elaboration on this. I'm using this:

Why is __strong required in fast enumeration loops with ARC

送分小仙女□ 提交于 2019-11-29 07:44:26
When I do something simialr to the following I get an error saying for (UIView* att in bottomAttachments) { if (i <= [cells count]) { att = [[UIView alloc] extraStuff] } } Fast Enumeration variables cannot be modified in ARC: declare __strong What does __strong do and why must I add it? If a variable is declared in the condition of an Objective-C fast enumeration loop, and the variable has no explicit ownership qualifier, then it is qualified with const __strong and objects encountered during the enumeration are not actually retained. Rationale This is an optimization made possible because