automatic-ref-counting

@property definitions with ARC: Is strong default now?

可紊 提交于 2019-11-28 22:04:20
问题 Just running the ARC refactoring tool on the new xcode 4.3.1 and noticed that my (nonatomic, retain) properties are being swapped out for just (nonatomic) instead of (nonatomic, strong) like in the previous xcode. My code seems to run fine after this change so I am assuming that it is defaulting to strong anyway, anyone know why things have changed with the ARC converter? 回答1: Yes, strong is the default in Xcode 4.3 and later. It's documented both in the LLVM docs and in Apple's guides to

CG Gradient runs on simulator, but not on iPhone

▼魔方 西西 提交于 2019-11-28 22:00:45
I have a code that compile without problems. It runs well on the iPhone simulator, but on my device, I get an EXC_BAD_ACCESS. This happens in a helper function to draw gradient. I followed this tutorial to do it. The code I have is as follows: - (void) drawRect:(CGRect)rect { CGContextRef context = UIGraphicsGetCurrentContext(); CGColorRef whiteColor = [UIColor whiteColor].CGColor; CGColorRef lightGrayColor = [UIColor colorWithRed:230.0/255.0 green:230.0/255.0 blue:230.0/255.0 alpha:1.0].CGColor; CGColorRef separatorColor = [UIColor colorWithRed:208.0/255.0 green:208.0/255.0 blue:208.0/255.0

Discovering at runtime which of a class' instance variables are declared __weak

为君一笑 提交于 2019-11-28 21:54:28
I'm writing a tool which would benefit from knowing which of a class' instance variables are declared __weak . This information must exist somewhere at runtime, but is there any way of accessing it, documented or otherwise? (It's for a tool, so I don't care so much about it breaking with updates) Alright, here is a sample implementation, using a custom object implementation, that does a rudimentary check to see if an iVar is weak or not: BOOL iVarIsWeak(Class cls, Ivar ivar) { id classInstance = [cls new]; // our custom base class properly tracks reference counting, no weird voodoo id

What is the correct way to declare a readonly property for ios using ARC

喜你入骨 提交于 2019-11-28 19:34:04
I am new to iOS development in general and have never dealt with manual reference counting (retain, release, autorelease). As such I don't have a good understanding of what magic ARC is performing. I thought I understood until I was asked what type of ownership ( weak , strong , assign , etc) should be given to a readonly property pointing at an object, such as: @property (readonly,nonatomic) NSString* name; I read here Questions about a readonly @property in ARC that leaving off the strong / weak won't actually compile unless you specify a backing variable when you @synthesize the property; I

Should I still copy/Block_copy the blocks under ARC?

耗尽温柔 提交于 2019-11-28 19:21:53
I've just stumbled over the following SO topic: Why should we copy blocks rather than retain? which has the following sentence: However, as of iOS 6 they are treated as regular objects so you don't need to worry. I was really confused by this assertion that is why I am asking: does this assertion really imply that Objective-C developers do not need to @property (copy) blockProperties or [^(...){...) {} copy] to copy blocks and their contents from stack to heap anymore? I hope the description I've made is clear. Please, be verbose. Similar questions Under ARC, are Blocks automatically copied

Capturing 'self' strongly in this block is likely to lead to a retain cycle [duplicate]

[亡魂溺海] 提交于 2019-11-28 18:48:25
This question already has an answer here: capturing self strongly in this block is likely to lead to a retain cycle 7 answers I have reqest with block. But the compiler issues a warning "Capturing 'self' strongly in this block is likely to lead to a retain cycle" __weak typeof(self) weakSelf = self; [generalInstaImage setImageWithURLRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:data[@"images"][@"low_resolution"][@"url"]]] placeholderImage:[UIImage imageNamed:@"Default"] success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) { NSLog(@"success");

Defining Objective-C blocks as properties - best practice

我只是一个虾纸丫 提交于 2019-11-28 17:59:16
I've recently come across an Apple document that shows the following property declaration for a block: @interface XYZObject : NSObject @property (copy) void (^blockProperty)(void); @end Also, this article states: Note: You should specify copy as the property attribute, because a block needs to be copied to keep track of its captured state outside of the original scope. This isn’t something you need to worry about when using Automatic Reference Counting, as it will happen automatically, but it’s best practice for the property attribute to show the resultant behavior. For more information, see

Check for ARC in precompile

半世苍凉 提交于 2019-11-28 17:29:10
I have an iOS refactoring library that I want to work with and without the ARC compilation option. Is there a way to detect during compilation, like with an #ifdef , if ARC is available? Yes, you can use the following: #if __has_feature(objc_arc) ... #endif Even if you're using the latest version of LLVM, this will only evaluate to true if you're compiling with the -fobjc-arc option. 来源: https://stackoverflow.com/questions/7708267/check-for-arc-in-precompile

Recursive Block Retain Cycles

萝らか妹 提交于 2019-11-28 17:17:19
Will this lead to any sort of retain cycle? Is it safe to use? __block void (^myBlock)(int) = [^void (int i) { if (i == 0) return; NSLog(@"%d", i); myBlock(i - 1); } copy]; myBlock(10); myBlock = nil; Your code does contain a retain cycle, but you can break the retain cycle at the end of the recursion by setting myBlock to nil in the recursion base case ( i == 0 ). The best way to prove this is to try it, running under the Allocations instrument, with “Discard unrecorded data upon stop” turned off, “Record reference counts” turned on, and “Only track active allocations” turned off. I created a

recursive block and retain cycles in ARC

人走茶凉 提交于 2019-11-28 17:15:34
问题 EDIT2: No. The suggested answer is about async calls. I want & need synchronous calls, like in a normal, standard recursive call. EDIT: while __unsafe_unretained void (^unsafe_apply)(UIView *, NSInteger) ; compiles without warning or errors, it fails at runtime with a NULL stored into unsafe_apply. However this: - (void) applyToView: (UIView *) view { UIColor * (^colorForIndex)(NSInteger) = ^(NSInteger index) { return [UIColor colorWithHue: ((CGFloat) index / 255.0f) saturation: 0.5f