objective-c-blocks

Assigning a swift closure (block equivalent) to an existing objective-c block being accessed using a bridge

醉酒当歌 提交于 2019-12-13 06:14:28
问题 I am using Swift and was wondering if there's a way I can assign a closure to an existing objective-c block. fromObjC?.performBlock = {someVar in /*do something*/} It gives me an error "Cannot assign to the result of this expression". 回答1: All pointers to objects in objective-C must be Optional in swift because a pointer can be nil. If you know that the variable will never actually be nil, you should use Implicitly Unwrapped Optionals ( TypeName! ) so that you don't have to unwrap it. So void

how to fire block event in objective c when uiviewcontroller dealloc

坚强是说给别人听的谎言 提交于 2019-12-13 04:41:19
问题 How to fire block event in Objective C when UIViewController dealloc. For example : [PGMemberObj requestWithUserName:@"ab" andPassword:@"cc" andCallback:^(BOOL isSuc){ if (isSuc) { NSLog("Login Suc."); }else { NSLog("Login Failed"); } }]; when i pop ViewController and dealloc was executed,i still receive Login Suc. or Login Failed Message. How to avoid this issue? 回答1: Try with the following code: __weak UIViewController *weakSelf = self; [PGMemberObj requestWithUserName:@"ab" andPassword:@

Incrementing a Variable from an Asynchronous Block in Objective-C

夙愿已清 提交于 2019-12-13 02:56:10
问题 I have run into a bit of a conundrum with a service I am working on in objective-c. The purpose of the service is to parse through a list of core-data entities and download a corresponding image file for each object. The original design of the service was choking my web-server with too many simultaneous download requests. To get around that, I moved the code responsible for executing the download request into a recursive method. The completion handler for each download request will call the

Problems with Reachability Apple class

不打扰是莪最后的温柔 提交于 2019-12-13 02:38:29
问题 I have an App that uses UITabBar and it has to download contents from the Internet, so I decided to use the class Reachability. When I launch it, the method works greatly, but if I don't wait that all the job is done and I go to another tabBar index, then I go back to the first one, the App holds on and doesn't move. Here's some code: - (void)viewWillAppear:(BOOL)animated { [[self.navigationController navigationBar] setHidden:YES]; [[NSNotificationCenter defaultCenter] addObserver:self

addObserverForName and removing observer

蹲街弑〆低调 提交于 2019-12-13 02:22:42
问题 On Cocoa code with ARC enabled, I tried to observe Window closing events like below. ScanWindowController * c = [[ScanWindowController alloc] initWithWindowNibName:@"ScanWindowController"]; [scanWindowControllers addObject:c]; [c showWindow:nil]; NSMutableArray *observer = [[NSMutableArray alloc] init]; observer[0] = [[NSNotificationCenter defaultCenter] addObserverForName:NSWindowWillCloseNotification object:nil queue:nil usingBlock:^(NSNotification *note) { [scanWindowControllers

Enterprise App Crashes after Install from MDM

隐身守侯 提交于 2019-12-13 01:28:24
问题 We have an iOS 9.2 app that runs fine in testing when installed via Xcode and the local Mac. When we install this application through our MDM server it crashes on the first access of data. It is exiting with the error "failed to scene-update after 10.00s." I from checking the console output, it appears to be hanging up after the success process in the requireLogin method (after the [self checkIntSession:self.sessionDetail]. We are using Novell's MDM not the Apple version. I have created other

iOS - Getting a variable from inside a block passed as a parameter

纵然是瞬间 提交于 2019-12-13 00:45:19
问题 I've been banging my head on this for a while now, but couldn't seem to find what I was looking for. Basically I have the following method declaration in class A. - (void)doSomethingCoolWithThisBlock:(void (^)(void))block In class B, when I call this method something like the following in the block part: UILabel *myLabel = [[UILabel alloc] init]; UITextField *myField = [[UITextField alloc] init]; etc. My question is, in the implementation of my doSomethingCoolWithThisBlock how can I dissect

ARC capturing self… block inside a block and reference being released before execution

柔情痞子 提交于 2019-12-13 00:25:06
问题 I have this problem: a block inside a block. self.createStuff = ^ (NSString *text) { self.post.onCompletion = ^(NSURLResponse *response, NSData *data, NSError *error){ [self doStuff]; // error here }; [self doMoreStuff]; // error here }; I will have errors in [self doStuff] and on [self doMoreStuff]. The error is capturing 'self' strongly in this block is likely to lead to a retain cycle Easy you say, just add id mySelf = self; before the first block and use mySelf instead. Nope. This will

code blocks with Grand Central Dispatch

孤者浪人 提交于 2019-12-12 23:22:02
问题 I'm writing an app using ARC, and I'm wondering if the following will cause a problem. Specifically, I'm creating an object 'A', then using GCD I add a code block to the main thread's queue and in the code block I perform some operations on the weak reference to the object. But by the time the code block is executed, my object has already been nullified. Since the code block only has a weak reference to the object, will this cause a problem? Or does the compiler somehow know to keep a

Has xcode 4.5 changed sortedArrayUsingComparator + blocks?

大兔子大兔子 提交于 2019-12-12 19:04:02
问题 Just updated xcode to 4.5 and I'm receiving an error in one of my iOS apps which I wasn't getting previously. Problem was not occurring before the update. Basically, I have an array that needs sorting, based on some other irrelevant tests.. NSArray *sortedArray = [arrayFiles sortedArrayUsingComparator:^(id a, id b) { NSString *first = [(PPFile*)a name]; NSString *second = [(PPFile*)b name]; if ([a isFileAvailableForRead] && ![b isFileAvailableForRead]) { return NSOrderedAscending; }else if(!