Tag:

如何立即在一个函数中执行一个块?

我在网上search,但我发现不是我所期望的。 我有一个块里面的函数,这个函数在处理之前返回到块中。 所以我的函数返回零… NSString* returnTheFolder() { NSUserDefaults* userDefaults = [NSUserDefaults standardUserDefaults]; GTLServiceDrive *service; __block NSMutableDictionary* titlesAndIdentifiers; __block __strong NSString* rootFolder; __block NSArray* allIdentifiers; NSString* userDefaultValue = [userDefaults objectForKey:@"User1"]; titlesAndIdentifiers = [[NSMutableDictionary alloc]init]; service = [[GTLServiceDrive alloc] init]; service.authorizer = [GTMOAuth2ViewControllerTouch authForGoogleFromKeychainForName:KeychainForName clientID:ClientID clientSecret:ClientSecret]; GTLQueryDrive *query = [GTLQueryDrive queryForFilesList]; query.maxResults = 9999999; query.q = @"'root' in […]

如何传递一个块作为select器与参数performSelector:withObject:?

是否有可能创build一个块variables作为select器接受参数,并将其传递给performSelector:withObject:? 目前,写一些testing,并希望写一个自包含的方法与所有相关的检查。 需要通过一个块,接收它的参数,并在该块做一些检查。 我正在寻找像这样的东西: … SEL blockAsSelector = ^{(NSString *param){NSLog(@"Passed param = %@", param);}} [self performSelector:blockAsSelector withObject:stringThatWillBeUsedAsParamInBlock]; …

ios fmdb内存泄漏

嗨我问这个问题,我的朋友,正在努力解决这个FMDB内存问题。 他的项目从一个数据库中读取一个列表,他打开数据库并读取每个单元格的数据(这不是一个好主意,但他做了),在init中,init数据库队列, databaseQueue = [[FMDatabaseQueue alloc] initWithPath:_dbPath]; 在cellForRowAtIndexPath中,使用configCell来做事情 – (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString * addressBookTableViewCellIdentifier = @"AddressBookTableViewCell"; AddressBookTableViewCell * cell = (AddressBookTableViewCell*)[tableView dequeueReusableCellWithIdentifier:addressBookTableViewCellIdentifier]; if (cell == nil) { // Create a cell to display an ingredient. cell = [[[AddressBookTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:addressBookTableViewCellIdentifier] autorelease]; cell.selectionStyle = UITableViewCellSelectionStyleBlue; } [self configCell:cell atIndexPath:indexPath]; return cell; […]

debugging和发布configuration之间的不同的块行为

我的程序完美地工作。 我向你保证,我的生活,0个错误。 自豪地,我试图将应用程序打包成一个.ipa文件,用于临时分发到使用TestFlight的Betatesting。 该scheme没有工作。 应该发生的animation从未发生。 networking代码中断。 美妙地淡出音乐的button根本没有做任何事情。 事实certificate,罪魁祸首是新的和shiny的块。 当我在模拟器或我的设备上testing我的程序时,我使用了默认的“Debug”构buildconfiguration。 但是,当我将其存档以供分发(并且我相信稍后将其提交给App Store)时,XCode使用另一个“Release”configuration。 进一步调查,不同之处在于优化级别(可以在XCode的Build Settings中find它):debugging使用无(-O0),但版本使用最快,最小(-Os)。 我不知道,它是最快,最小,不起作用(tm)。 是的,这两个configuration之间的块的行为不同。 所以,我着手解决这个问题。 我已经简化了我的即将改变世界的应用程序到它的裸骨,显示在我附加到这个职位的图像。 视图控制器有一个初始值为0的实例variablesx。如果我们按b,它会产生一个线程,它会不断地检查x的值,当x变为1时,改变底部的标签。我们可以使用button来改变x的值一个。 这是我的天真代码(我使用ARC btw): @implementation MBIViewController { int _x; } – (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. _x = 0; } – (void)updateLabel { self.topLabel.text = [NSString stringWithFormat:@"x: […]

阻止和保留周期不能赶上它

我有一个块和弱引用的问题,我在ARC下。 我build立了一个类,它是一个免费的项目,是一个围绕Google Directions API的简单包装,你可以在这里下载: link to the project 我在视图控制器内使用它的问题是,使用它后,视图控制器不会被释放。 我想这是这个对象的问题,因为如果我注释掉或设置为零,一切正常。 我无法理解保留周期在哪里,当然我设置为弱自我,这里是我使用它的视图控制器的方法: – (void) getDirections{ __weak RouteMapViewController * weakSelf = self; self.routeObject = [[RouteDirectionsObject alloc]init]; [self.mapView removeAnnotations:self.mapView.annotations]; [self.mapView removeOverlays:self.mapView.overlays]; [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES]; [_routeObject createDirectionRequestWithStartPoint:weakSelf.startPoint andEndPoint:weakSelf.endPoint withCallBackBlock:^(NSError *error, NSDictionary *routeDistance, NSDictionary *routeDuration, MKPolyline *routePolyline, NSArray *routes, NSArray *steps, CLLocation *startPoint, CLLocation *endPoint, NSArray *directions) { [[UIApplication sharedApplication] […]

OpenWithCompletionHandler完成后如何返回结果

想要查询Coredata数据库中的照片 这是我的代码 这是NSObjectSubclass类别 //Photo+creak.h #import "Photo+creat.h" @implementation Photo (creat) +(Photo *)creatPhotoByString:(NSString *)photoName inManagedObjectContext:(NSManagedObjectContext *)context{ Photo *picture = nil; NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Photo"]; request.predicate = [NSPredicate predicateWithFormat:@"name = %@", photoName]; NSArray *matches = [context executeFetchRequest:request error:nil]; if (!matches || [matches count]>1) { //error } else if ([matches count] == 0) { picture = [NSEntityDescription insertNewObjectForEntityForName:@"Photo" […]

asynchronous数据连接iOS

我为当前正在编写的应用程序编写了一个同步类。 由于数据量很大,它首先得到一个数据计数,然后在NSOperationQueue批量下载。 这一切工作正常,我有快速工作的同步algorithm。 它的工作方式如下… – (void)synchroniseWithCompletionHandler://block for completion handler errorHandler://block for error handler { [self.queue addOperationWithBlock ^{ //Create an NSURLRequest for the first batch //Send the request synchronously //Process the result //If error then cancel all operations in the queue, run errorHandler and return. }]; [self.queue addOperationWithBlock ^{ //Create an NSURLRequest for the second batch […]

在View Controller之后,iOS块阻止exceptioncallback导致崩溃

我有一个singleton类与一个方法,成功和失败块作为参数,它调用另一个asynchronous执行的方法,也使用成功和失败块。 我的方法的成功块被asynchronous方法的成功块调用。 一切都很好,除非我的视图控制器在成功块返回之前被取消分配,在这种情况下,应用程序崩溃。 这种情况似乎类似于在dealloc方法中将委托设置为零。 我应该如何处理这个块? 这是我的代码看起来像: – (void)getObjectsWithId:(NSInteger)id success:(void (^)(NSArray *objects))success failure:(void (^)(NSInteger statusCode, NSError *error))failure { NSString *path = [NSString stringWithFormat:@"/my_objects/%d/objects", id]; [self getPath:path parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) { NSMutableArray *objects = [[NSMutableArray alloc] initWithCapacity:[responseObject count]]; for (NSDictionary *dict in responseObject) { Object *object = [[Object alloc] initWithDictionary:dict]; [objects addObject:object]; } success(objects); } […]

使用RestKit,使用block来加载对象,何时以及如何取消请求?

[[RKObjectManager sharedManager] loadObjectsAtResourcePath:@"app/site_pattern" usingBlock:^(RKObjectLoader* loader) { [loader setObjectMapping:clientMappring]; loader.delegate = self; shopLoader = loader; }]; 以上,我使用块函数来加载我的应用程序中的一些数据,但是当我popup这个viewcontroller,我不知道何时以及如何取消这个请求。 任何想法? – (void)showSelectShop { SelectShopViewController * selectShopViewController = [[SelectShopViewController alloc] initWithNibName:@"SelectShopViewController" bundle:nil]; [self.navigationController pushViewController:selectShopViewController animated:YES]; } 更多: 我尝试在viewDidUnload中取消它 – (void)viewDidUnload { [super viewDidUnload]; [shopLoader cancel]; } 但它没有工作。 我仍然有错误。

NSLinguisticTagger内存泄漏

我一直在用iOS 5.0的新的NSLinguisticTagger在Xcode 4.2中摆弄。 我用这个函数的目标是获取一个地址簿logging,然后把一个合成名称作为一个NSString来分发,就像ABRecordCopyCompositeName所做的一样,但是考虑到东亚语言和匈牙利语的命名顺序(倒数第一,而不是倒数第一) )。 这个function: NSString *text = [self getLocalizedFullNameOfRecord:[contacts objectAtIndex:indexPath.section]; – (NSString *) getLocalizedFullNameOfRecord:(ABRecordRef) person { NSString *firstName = ABRecordCopyValue(person, kABPersonFirstNameProperty); NSString *middleName = ABRecordCopyValue(person, kABPersonMiddleNameProperty); NSString *lastName = ABRecordCopyValue(person, kABPersonLastNameProperty); NSString *prefix = ABRecordCopyValue(person, kABPersonPrefixProperty); NSString *suffix = ABRecordCopyValue(person, kABPersonSuffixProperty); NSString *fullName = @""; __block BOOL Asian; // Apologies to all Hungarians […]