【发布时间】:2010-07-30 15:37:17
【问题描述】:
我有一个委托类来处理来自 CLLocationManager 的响应并通过 printf() 打印它们。是否可以在 main() 中放入某种类型的繁忙循环,以便程序保持打开状态并保持 CLLocationManager 连接到 Delegate 愉快地处理事件?
#import <Foundation/Foundation.h>
#import "Delegate.h"
#import <CoreLocation/CoreLocation.h>
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
Delegate *del = [Delegate alloc];
CLLocationManager *locationManager;
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = del;
[locationManager startUpdatingLocation];
// Something goes here
[pool drain];
return 0;
}
【问题讨论】:
-
如果
CLLocationManager在另一个线程上运行它很酷的东西,而你只需要一些东西来保持你的程序活着,你可以使用NSApplicationMain(argc, argv)或dispatch_main()(来自<dispatch/dispatch.h>),在你方便的时候.尽管dispatch_main永远不会返回,所以[pool drain]永远不会被调用。 -
使用 NSRunLoop *runLoop = [NSRunLoop currentRunLoop]; [runLoop 运行];
标签: objective-c cocoa core-location