小编Pai*_*Pol的帖子

如何包装一个异步方法,该方法接受一个块并在目标c中将其转换为同步

我想包装一个如下所示的异步API:

[someObject completeTaskWithCompletionHandler:^(NSString *result) {

}];
Run Code Online (Sandbox Code Playgroud)

进入一个我可以像这样调用的同步方法

NSString *result = [someObject completeTaskSynchronously];
Run Code Online (Sandbox Code Playgroud)

我该怎么做呢?我做了一些文档阅读和谷歌搜索,并尝试使用"dispatch_semaphore"尝试实现它,如下所示:

-(NSString *) completeTaskSynchronously {
   __block NSString *returnResult;
   self.semaphore = dispatch_semaphore_create(0);  
   [self completeTaskWithCompletionHandler:^(NSString *result) {
       resultResult = result;
       dispatch_semaphore_signal(self.semaphore);
   }];

   dispatch_semaphore_wait(self.semaphore, DISPATCH_TIME_FOREVER);
   return resultResult;
}
Run Code Online (Sandbox Code Playgroud)

但这似乎没有用,它基本上只是停在dispatch_semaphore_wait.执行永远不会到达执行_signal的内部块.任何人都有关于如何做到这一点的代码示例?我怀疑该块必须在主线程的其他线程上?此外,假设我无法访问异步方法背后的源代码.谢谢!

multithreading asynchronous objective-c grand-central-dispatch ios

12
推荐指数
3
解决办法
4333
查看次数

mac删除后自动选择文本

运行OS X 10.9,虽然问题也发生在10.8,我在Xcode和Netbeans中注意到了它.当我突出显示某些文本并将其删除时,通常是某些代码中的变量或字符串,所选区域保持选中状态,基本上选择下一组字符.因此,当我继续输入时,我输入了一些选定的代码.

例如,假设我在括号中选择了以下代码:快速[棕色狐狸]跳过懒狗.

现在删除文本后,将选中括号之间的以下文字:快速[跳跃]懒狗.

预期的结果是没有选择文本,光标只是在单词跳转中的'J'(或之前的空格).

我在Netbeans和Xcode中搜索过这种类型的行为,但是没有发现任何东西,所以我得出的结论是OS X中的行为,但是我还没有找到其他人这个问题,也没有找到解决方法.

感谢您的任何帮助.

macos highlighting

8
推荐指数
1
解决办法
505
查看次数

ios,startMonitoringSignificantLocationChanges - 有时会破坏监控

我有更新用户位置的奇怪问题.有时我的应用程序会更新位置,但有时会更新.我不知道问题出在哪里,我的代码(来自AppDelegate.m)如下:

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    NSLog(@"Went to Background");
    // Only monitor significant changes
    [locationManager startMonitoringSignificantLocationChanges];
}


- (void)applicationDidBecomeActive:(UIApplication *)application
{
    // Start location services
    locationManager = [[CLLocationManager alloc] init];
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;

    // Only report to location manager if the user has traveled 1000 meters
    locationManager.distanceFilter = 1000.0f;
    locationManager.delegate = self;
    locationManager.activityType = CLActivityTypeAutomotiveNavigation;

    [locationManager stopMonitoringSignificantLocationChanges];
    [locationManager startUpdatingLocation];
}


- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
    // Check if running in background or not
    BOOL isInBackground = NO;
    if ([UIApplication sharedApplication].applicationState …
Run Code Online (Sandbox Code Playgroud)

cllocationmanager ios

7
推荐指数
1
解决办法
1万
查看次数

如何在iOS 7中以编程方式将UIViewControllerBasedStatusBarAppearance更改为YES/NO?

我的应用程序背景较暗,但在iOS 7中,状态栏变得透明.所以我看不到任何东西,角落里只有绿色电池指示灯.如何将状态栏文本颜色更改为绿色或橙色,就像在主屏幕上一样?

我知道

  1. 在plist中设置UIViewControllerBasedStatusBarAppearancetoYES

  2. viewDidLoad做一个[self setNeedsStatusBarAppearanceUpdate];

  3. 添加以下方法:

    -(UIStatusBarStyle)preferredStatusBarStyle{ 
        return UIStatusBarStyleLightContent; 
    }
    
    Run Code Online (Sandbox Code Playgroud)

如何以UIViewControllerBasedStatusBarAppearance编程方式更改?

提前致谢...

iphone uistatusbar ios7

5
推荐指数
2
解决办法
1万
查看次数

表情符号到 ios 中的十六进制值

我想知道如何将表情符号转换为十六进制值。那是对于具有“张开嘴和微笑的眼睛的笑脸”的笑脸笑脸对应于:))我应该得到“1F604”

ios emoji

3
推荐指数
1
解决办法
2888
查看次数

每个自定义GCD队列都需要ARC下的自动释放池吗?

例如,如果您创建自己的GCD队列:

self.renderQueue = dispatch_queue_create("com.test.queue", DISPATCH_QUEUE_SERIAL);
Run Code Online (Sandbox Code Playgroud)

您是否必须在提交到该队列的每个块中创建自动释放池,使用:

@autoreleasepool {

}
Run Code Online (Sandbox Code Playgroud)

或者ARC是否为您创建了它?如果您未在自定义队列中指定autoreleasepool,会发生什么?

memory-management grand-central-dispatch

3
推荐指数
1
解决办法
722
查看次数

cellForRowAtIndexPath的替代方案

我正在寻找cellForRowAtIndexPath的替代方法.我想要做的是在我的私有方法中,我正在检查退出单元格上的文本是否存在于数组中,如果是,则选择其他单元格保持取消选择.我了解到cellForRowAtIndexPath仅对可见单元格有效,如果单元格不可见则返回nil.以下是我目前的代码:

- (void)selectCells:(NSArray *)array
{
    for (int row = 0; row < [self.tableView numberOfRowsInSection:1]; row ++)
    {
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row 1];

        UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];

        if ([array containsObject:cell.textLabel.text] && !cell.isSelected)
        {
           [self.tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我还能用什么来实现这个目标?

cocoa-touch uitableview ios

0
推荐指数
1
解决办法
323
查看次数