2010-01-28 21 views
18

क्या कोई भी एनआईबी फाइल लोड किए बिना NSRunLoop प्रारंभ करना संभव है (यानी NSApplicationMain() पर कॉल किए बिना)?कोको कमांड लाइन प्रोग्राम में NSRunLoop चलाएं

धन्यवाद।

+0

की तरह बंद कर सकते हैं क्यों आप एक CLI अनुप्रयोग में एक रन पाश चाहते हैं? – kennytm

+0

आप क्लि ऐप में एनआईबी फाइल क्यों लोड करना चाहते हैं? – bertolami

+1

बर्टोलामी: मुझे लगता है कि उसका मतलब * बिना * था। –

उत्तर

11

हां; आप NSApplicationMain से वापस लौटने के बिना अपनी मुख्य विधि लिख सकते हैं और NSRunLoop चला सकते हैं।

इस link पर एक नज़र डालें; यह लड़का अपनी मुख्य विधि में NSRunLoop का उपयोग कर रहा है, हालांकि वह एनआईबी फाइल लोड नहीं कर रहा है, लेकिन इसे आपको NSRunloops के साथ जाना चाहिए।

+0

का भी उपयोग कर सकते हैं यह भी देखें: आधिकारिक [NSRunLoop दस्तावेज़] (https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSRunLoop_Class) – lindes

14

समाधान मैन्युअल रूप से एनएसएप्लिकेशन का आह्वान करना है। अपने अनुप्रयोग प्रतिनिधि बनाएं पहले) की तुलना में बदलने के NSApplicationMain (निम्नलिखित के साथ main.m में कहते हैं:

AppDelegate * delegate = [[AppDelegate alloc] init]; 

NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; 

NSApplication * application = [NSApplication sharedApplication]; 
[application setDelegate:delegate]; 
[NSApp run]; 

[pool drain]; 

[delegate release]; 

प्रतिनिधि, जब तैयार द्वारा सक्रिय किया जाएगा एक निब

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification 
+1

कोई जरूरी आवश्यकता नहीं है एनएसएप्लिकेशंस का उपयोग करने के लिए (और इस प्रकार ऐपकिट पर निर्भरता लागू करें); यह सब NSRunLoop के साथ किया जा सकता है। इस प्रकार, जबकि यह किसी के लिए संभावित रूप से उपयोगी जानकारी है, मुझे लगता है कि यह इस प्रश्न का अनुचित उत्तर है। उस कारण के लिए नीचे मतदान। – lindes

+0

मैं [[NSRunLoop currentRunLoop] रन] का उपयोग कर रहा था; मेरे जीयूआई-कम आवेदन में, लेकिन मेरा आवेदन राज्य का जवाब नहीं दे रहा था। यह विधि मेरे लिए ठीक काम कर रही है। –

+0

एआरसी – user2159978

5

की जरूरत के बिना पर एक नज़र डालें asynctask.m जो NSRunLoop मैन्युअल रूप से चलाता है ताकि एसिंक्रोनस "waitForDataInBackgroundAndNotify" अधिसूचनाओं का उपयोग सक्षम हो सके।

http://www.cocoadev.com/index.pl?NSPipe

NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init]; 

    while(!terminated) 
    { 
    //if (![[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:100000]]) 
    if (![[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]]) 
    { 
     break; 
    } 
     [pool release]; 
     pool = [[NSAutoreleasePool alloc] init]; 
    } 

    [pool release]; 
8
// Yes. Here is sample code (tested on OS X 10.8.4, command-line). 
// Using ARC: 
// $ cc -o timer timer.m -fobjc-arc -framework Foundation 
// $ ./timer 
// 

#include <Foundation/Foundation.h> 

@interface MyClass : NSObject 
@property NSTimer *timer; 
-(id)init; 
-(void)onTick:(NSTimer *)aTimer; 
@end 

@implementation MyClass 
-(id)init { 
    id newInstance = [super init]; 
    if (newInstance) { 
     NSLog(@"Creating timer..."); 
     _timer = [NSTimer scheduledTimerWithTimeInterval:1.0 
      target:self 
      selector:@selector(onTick:) 
      userInfo:nil 
      repeats:YES]; 
    } 
    return newInstance; 
} 

-(void)onTick:(NSTimer *)aTimer { 
    NSLog(@"Tick"); 
} 
@end 

int main() { 
    @autoreleasepool { 
     MyClass *obj = [[MyClass alloc] init]; 
     [[NSRunLoop currentRunLoop] run]; 
    } 
    return 0; 
} 
6

के लिए डॉक्स में अनुशंसाओं का पालन करें [NSRunLoop चलाने]:

BOOL shouldKeepRunning = YES;  // global 
NSRunLoop *theRL = [NSRunLoop currentRunLoop]; 
while (shouldKeepRunning && [theRL runMode:NSDefaultRunLoopMode beforeDate:[NSDate  distantFuture]]); 
12

स्विफ्ट में, आप के अंत में निम्न पंक्ति जोड़कर इस लक्ष्य को हासिल कर सकते हैं आपका main.swift:

NSRunLoop.currentRunLoop().run(); // Swift < 3.0 
RunLoop.current.run();    // Swift >= 3.0 

यदि आप रन लूप को रोकने में सक्षम होना चाहते हैं तो आपको कोर फाउंडेशन विधियों का उपयोग करना होगा।

CFRunLoopRun(); // start 

और आप इसे इस

CFRunLoopStop(CFRunLoopGetCurrent()); // stop 
+2

यह बहुत अच्छा है ... अब मुझे नहीं पता कि कैसे रुकना है, यद्यपि। क्या आपके पास मेरे लिए कोई सुझाव हैं? – cdf1982

+1

मेरा जवाब अपडेट किया गया –

संबंधित मुद्दे