Tag: objective c blocks

在数组块枚举中创buildUITableViews会导致崩溃

所以故事就这样:) 我试图阻止枚举NSArray中的对象,并dynamic创build每个UITableViews并将其添加到UIScrollView中。 为了可读性和可重用性,我使用www.objc.io中的Lighter View Controller 。 dataSource被分别为每个UITableView创build。 问题是我一直崩溃 -[NSObject(NSObject) doesNotRecognizeSelector:] 我从堆栈上的post中发现,块枚举中的对象由于速度问题而保留较弱,并且可以确认dataSource实际上是为每个表取消分配的。 我甚至试图用__strong初始化ArrayDataSource,但是没有任何效果。 __strong ArrayDataSource *customdayTableDataSource = [[ArrayDataSource alloc] initWithConfigureCellBlock:configureCell cellIdentifier:DayTableCellIdentifier]; 我在做什么错在块? 你能指点我正确的方向吗? TableViewCellConfigureBlock configureCell = ^(id cell, id object) { [cell configureForObject:object]; }; [NSArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { int tableHorizontalPosition = [[UIScreen mainScreen] bounds].size.width * idx; int tableHeight = [[UIScreen mainScreen] bounds].size.height; […]

使用sendAsynchronousRequest:queue:completionHandler:用于URL请求

我正在开发另一个开发人员创build的iOS应用程序的更新。 他使用ASIHTTPRequest处理http请求。 但是,我的应用程序版本必须与崩溃。 由于ASIHTTPRequest不再被更新,所以我认为我应该改用AFNetworking,但对于我来说这太复杂了。 所以最后我以为我可以自己使用NSURLConnection。 这是我想要replace的原始代码: -(NSArray*)readUTF16LEFeed:(NSURL*) urlToRead{ ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:urlToRead]; [request startSynchronous]; NSError *error = [request error]; if (!error) { lastModified = [NSDate date]; NSData *response = [request responseData];//UTF-16LE NSString* responseString = [[NSString alloc] initWithData:response encoding:NSUTF16LittleEndianStringEncoding]; DLog(@"string is: %@",responseString); responseString = [responseString stringByReplacingOccurrencesOfString:@"ISO-8859-1" withString:@"UTF16-LE"]; NSData* data = [responseString dataUsingEncoding:NSUTF16LittleEndianStringEncoding]; return [self parseNamesFromXML:data]; […]

在Objective-C中混合模块和委托

代理收到消息时是否可以运行代码块? 例如,如果我有一个框架,把一个void块作为参数(我们将其称为“成功”块),并使用NSURLConnection委托做这些方法参数的东西,当我收到来自网页,我怎样才能调用方法parameter passing的“成功”块? 这真的很难解释,而且我显然没有任何代码,但是我可以澄清你是否有任何问题。

如果方法使用NSURLConnection如何返回输出与块的asynchronous调用

我正在向服务器发送请求以使用NSURLConnection块方法获得响应。 我不能使用返回YES或在块中返回NO。 我怎样才能解决这个问题? -(BOOL)checkForValidUrl:(NSString *)url { __block int httpStatus=0; NSMutableURLRequest *request=[[NSMutableURLRequest alloc]initWithURL:[NSURL URLWithString:url]]; [request setValue:@"application/json" forHTTPHeaderField:@"content-type"]; [request setValue:@"application/json" forHTTPHeaderField:@"Accept"]; [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *responseData, NSError *error) { httpStatus = [((NSHTTPURLResponse *)response) statusCode]; NSLog(@"httpStatus inside block:%d",httpStatus); } ]; return NO; }

如何在uiviewcontroller释放时在目标c中触发块事件

如何在UIViewController dealloc时触发Objective C中的块事件。 例如 : [PGMemberObj requestWithUserName:@"ab" andPassword:@"cc" andCallback:^(BOOL isSuc){ if (isSuc) { NSLog("Login Suc."); }else { NSLog("Login Failed"); } }]; 当我popupViewController和dealloc被执行,我仍然收到loginSuc。 或login失败消息。 如何避免这个问题?

如何返回在delay_after块中确定的值?

对于Objective-C ,这是非常新颖的东西,而且要花费大量的时间来弄清楚如何完成以下任务 我来自javascript背景,所以我可能不会以正确的方式来处理这个问题。 在我的视图控制器中,我正在调用类方法getLuminosity 。 我想从相机中收集一些float 7秒,取平均值,然后返回平均值,但不知道如何去做。 这是我的getLuminosity代码: – (CGFloat) getLuminosity { … [vidCam startCameraCapture]; dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(7 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ [vidCam stopCameraCapture]; NSNumber *averageLumin = [_arrayOfLumins valueForKeyPath:@"@avg.self"]; return [averageLumin floatValue]; }); return floatFromDispatchBlock; } 谢谢你的帮助!

如何在iOS中asynchronous下载图像?

我想先创buildimageview到滚动view.there我设置的占位符图像或给imageview.so的背景颜色所有图像视图显示。 – (void)LoadDataInScrollView { dispatch_queue_t imageQueue = dispatch_queue_create("Image Queue",NULL); //totalImageGetDict into count total Number of object and generate imageview. for (int ivalue = 0; ivalue < [totalImageGetDict count]; ivalue++) { dispatch_async(imageQueue, ^{ //define x value CGFloat xOrigin =ivalue *ascrollView.frame.size.width+50; imageView = [[UIImageView alloc] init]; imageView.tag = ivalue; [imageView setBackgroundColor:[UIColor whiteColor]]; imageView.frame = CGRectMake(xOrigin-40,imgYpos, ascrollView.frame.size.width-20, scrollViewHight); […]

iOS – 连接asynchronous的基于块的操作

你将如何执行N个asynchronous操作,例如networking调用,处理完成块操作以及没有委托/通知? 鉴于N这样的方法: – (void)methodNWithCompletion:(void (^)(Result *))completion { Operation *operation = [Operation new]; // … // Asynchronous operation performed here // … return; } 一个简单的解决scheme是调用前一个完成块中的每个操作: [self method1WithCompletion:^(Result *result) { // … [self method2WithCompletion:^(Result *result) { // … [self method3WithCompletion:^(Result *result) { // … [self method4WithCompletion:^(Result *result) { NSLog(@"All done"); } } } } 但我正在寻找一个更优雅和可重用的解决scheme,更容易编写和维护(没有多less缩进块)。 非常感谢,DAN

目标C到Swift – DoWithBlock

我想只在一个目标C类函数完成时在Swift中创build一个数组。 我在目标C函数中使用了一个DoWithBlock,但是我是IOS新手,正在尝试解决它是否按照正确的顺序运行。 我在这里得到了解决scheme的帮助,但是我不确定我做了什么来解决这个问题是正确的,我相信这应该是一个新问题。 在目标C类中,我用这样的块调用函数: – (void)scanTableDoWithBlock:(void(^)(NSArray *scanResult, NSError *error))handler { AWSDynamoDBObjectMapper *dynamoDBObjectMapper = [AWSDynamoDBObjectMapper defaultDynamoDBObjectMapper]; AWSDynamoDBScanExpression *scanExpression = [AWSDynamoDBScanExpression new]; scanExpression.limit = @10; [[dynamoDBObjectMapper scan:[Mapper class] expression:scanExpression] continueWithBlock:^id(AWSTask *task) { if (task.error) { NSLog(@"The request failed. Error: [%@]", task.error); if (handler != nil) { handler(nil, task.error); } } if (task.exception) { NSLog(@"The request failed. Exception: […]

IOS中完成块的后台任务

我正在做IOS中的一些数据库操作。 基本上我想在后台线程中做到这一点。 我尝试使用GCD 。 但是对于我来说问题是我想在完成之后从这个过程中获得一些价值。 在向数据库插入项目之前说我检查项目是否已经存在。 请参阅下面的代码 __block Boolean isExisting = false; dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, (unsigned long)NULL), ^(void) { NSString *path = [SqliteManager initDatabase]; if(sqlite3_open([path UTF8String], &database) == SQLITE_OK) { NSString *query = [NSString stringWithFormat:@"select count (campaignId) from POTC where Id='%@' and taskid='%@' and pocId='%@' and userId='%@'",[submission.campaignId stringRepresentation],[submission.taskId stringRepresentation],[submission.pocId stringRepresentation],[[UUID UUIDWithString:submission.userId] stringRepresentation]]; const char *sql = [query […]