Tag: objective c blocks

iOS – 从作为parameter passing的块中获取variables

我已经有一段时间对此感到震惊,但似乎无法find我正在寻找的东西。 基本上我在A类中有下面的方法声明 – (void)doSomethingCoolWithThisBlock:(void (^)(void))block 在B类中,当我在块部分调用这个方法如下: UILabel *myLabel = [[UILabel alloc] init]; UITextField *myField = [[UITextField alloc] init]; 等等 我的问题是,在我的doSomethingCoolWithThisBlock的实现中,我怎么能剖析块内的东西,并得到说UILabel例如?

使用addObserverForName时保留周期:object:queue:usingBlock:

我是块编程新手。 我在我的Listener类中有下面的代码(不使用弧): – (void)someBlock:((void)^(NSDictionary *)myDictionary)myBlock { __block Listener *weakSelf = self; weakSelf = [[NSNotificationCenter defaultCenter] addObserverForName:@"MyNotification" object:nil queue:nil usingBlock:^(NSNotification *note) { //— Here have the retain cycles myBlock(note.userInfo); [[NSNotificationCenter defaultCenter] removeObserver:weakSelf name:@"MyNotification"]; }]; } 和我的DoMyStuff类: … some code Listener *myListener = [[[Listener alloc] init] autorelease]; [myListener someBlock:((void)^(NSDictionary *)myDictionary)myBlock{ [self.someProperty doSomething:myDictionary]; }]; 谁能告诉我正确的方向来解决保留周期? 我检查了这两个问题 “正确pipe理addObserverForName:object:queue:usingBlock:” “为什么不从NSNotificationCenter删除观察者:addObserverForName:usingBlock被调用” […]

我怎样才能保留一个块中设置的局部variables?

所以我有: @interface testAppControl : NSObject { NSString *s; } 然后在我的方块我想要做的 [SendAPI setGroupWithName:groupName completionHandler:^(NSArray *errors) { s = @"something"; }]; NSLog(@"%@", s); //<— s is null 所以我想在离开我的街区之后保留价值"something" 。 这可能与ARC?

如何在Swift中调用Objective-C块的属性

我正在重写Swift中的一些Objective-C代码。 它是一个名为ActionSheetPicker的开放库的子类。 所以这里是我的Objective-C代码: #import "XXActionSheetStringPicker.h" @implementation XXActionSheetStringPicker + (instancetype)pickerWithTitle:(NSString *)title rows:(NSArray *)strings initialSelection:(NSInteger)index doneBlock:(ActionStringDoneBlock)doneBlock cancelBlock:(ActionStringCancelBlock)cancelBlockOrNil origin:(id)origin { XXActionSheetStringPicker *picker = [[self alloc] initWithTitle:title rows:strings initialSelection:index doneBlock:doneBlock cancelBlock:cancelBlockOrNil origin:origin]; //do something specific stuff UIBarButtonItem *cancelBarButton = [self itemWithtitle:NSLocalizedString(@"GLOBAL_CANCEL", nil) action:@selector(cancelAction) target:picker]; [picker setCancelButton:cancelBarButton]; UIBarButtonItem *doneBarButton = [self itemWithtitle:NSLocalizedString(@"GLOBAL_OK", nil) action:@selector(doneAction) target:picker]; [picker setDoneButton:doneBarButton]; return picker; } […]

通过引用来访问实例variables是否安全?

下面的两个代码片段有什么区别: 1。 __block __weak NSMutableArray *arrBlock = self.arr ; [[AsyncRequest initRequest:url onCompletedBlock:^(NSMutableArray *arr) { arrBlock = arr; }]ExecuteRequest]; 2。 id __weak weakself = self; [[AsyncRequest initRequest:url onCompletedBlock:^(NSMutableArray *arr) { weakself.arr = arr; }]ExecuteRequest]; 他们都没有导致保留周期,但苹果build议使用第一个。 第二个有问题吗?

使用块将NSDictionary转换为string?

我相信有一种方法可以使用块来做到这一点,但我无法弄清楚。 我想把一个NSDictionary变成url风格的参数string。 如果我有一个NSDictionary看起来像这样: dict = [NSDictionary dictionaryWithObjectsAndKeys:@"blue", @"color", @"large", @"size", nil]]; 那我怎么把它变成一个看起来像这样的string: "color=blue&size=large" 编辑 感谢下面的线索。 这应该做到这一点: NSMutableString *parameterString; [dict enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) { [parameterString appendFormat:@"%@=%@&", key, obj]; }]; parameterString = [parameterString substringToIndex:[string length] – 1];

ObjectiveC块和variables

我有以下方法,使我的iOS应用程序(使用Restkit)的Web服务调用… BOOL valid = NO; RKObjectManager *objectManager = [RKObjectManager sharedManager]; NSString *servicePath = [WebServiceHelper pathForServiceOperation:[NSString stringWithFormat:@"/security/isSessionValid/%@", username]]; [objectManager getObjectsAtPath:servicePath parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) { BooleanServiceResponse *resp = [mappingResult firstObject]; valid = resp.value; } failure:^(RKObjectRequestOperation *operation, NSError *error) { NSLog(@"Error while validating session for user %@ : %@", username, error); }]; return valid; 但是,我在validvariables上得到一个错误,说它是在块之外声明的,并且是不可赋值的。 我search了一下,发现了一个build议,我宣布这样的有效,而不是… […]

在iOS上如何显示UIAlertView?

从块中显示UIAlertView的最好方法是什么? 我在我的代码中有以下操作: – (IBAction)connectWithTwitterClicked:(id)sender { ACAccountStore * account = [[ACAccountStore alloc]init]; ACAccountType * accountType = [account accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter]; [account requestAccessToAccountsWithType:accountType options:nil completion:^(BOOL granted, NSError *error) { if (granted == YES){ NSLog(@"Twitter account has been granted"); NSArray * arrayOfAccounts = [account accountsWithAccountType:accountType]; if([arrayOfAccounts count] > 0){ ACAccount * twitterAccount = [arrayOfAccounts lastObject]; NSLog(@"Found twitter Id of [%@]", […]

UIButton重复按下

我有一个UITableViewCell,我有一个UIButton。 每次按下button,都会有一个networking调用,它会更新一个标签(增加或减less计数),类似于Facebook的“概念”。 问题是当用户重复按下UIButton时,值不断递增或递减。 我试着切换userInteraction并设置setEnabled状态。 仍然没有工作。 然后我尝试使用块,因为这个链接build议。 还是行不通。 我很新的块。 有什么我在这里做错了吗? 这是没有块的实现: – (void) giveKarmaCheck:(BOOL)complete // network delegate after the update { NSLog(@"Completed!"); _karmaBeingGiven = NO; NSMutableDictionary *tempDict = [[NSMutableDictionary alloc] initWithDictionary:[[_questions objectForKey:@"questions"] objectAtIndex:_indexPath.section]]; int qKarma = [[tempDict objectForKey:@"qkarma"] integerValue]; NSString *karmaCountString; QXTHomeCell *questionCell = (QXTHomeCell*)[self.questionsTable cellForRowAtIndexPath:_indexPath]; if (_karmaGiven) { [tempDict setValue:[NSNumber numberWithInt:1] forKey:@"qkarmaStatus"]; qKarma++; [questionCell.karmaLogo setImage:[UIImage […]

如何更改块中的int值?

如何在块中更改int值,我有这样的: __block long long size = -1; ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *myasset) { ALAssetRepresentation * rep = [myasset defaultRepresentation]; size = [rep size]; //here showed normal value NSLog(@"needed size : %lld",size); }; ALAssetsLibrary* assetslibrary = [[[ALAssetsLibrary alloc] init] autorelease]; [assetslibrary assetForURL:self.tmpVideoURL resultBlock:resultblock failureBlock:nil]; //but here remaind -1 NSLog(@"out block value : %lld",size);