2009-10-20 12 views
6

के साथ एक CGPoint पास करने में समस्या मैं एक वर्ग में setPosition नामक विधि में अधिसूचना को आग लगाने की कोशिश कर रहा हूं, जो किसी अन्य वर्ग में setViewPointCenter को ट्रिगर करता है। हालांकि, मैं इसके साथ एक CGPoint भेजने की कोशिश कर रहा हूँ। लेकिन एक्सकोड इसे थोड़ा पसंद नहीं कर रहा है।कोको: NSNotification और NSDictionary

-(void)setPosition:(CGPoint)point 
{ 

    NSDictionary *dict = [[NSDictionary alloc] initWithObjectsAndKeys:@"sp", point, nil]; 

    [[NSNotificationCenter defaultCenter] 
    postNotificationName:@"SpriteDidSetPosition" 
    object:self 
    userInfo:dict]; 

    [super setPosition:point]; 
} 

एक और कक्षा में इस विधि का ट्रिगर है, लेकिन संकेत त्रुटि

-(id) init{ 

    // Usual stuff, blah blah blah... 

    [[NSNotificationCenter defaultCenter] 
    addObserver:self 
    selector:@selector(setViewPointCenter:) 
    name:@"BownceSpriteDidSetPosition" 
    object:nil]; 

} 

-(void) setViewPointCenter:(NSNotification *)notification 
{ 

    // ERROR: Invalid Initializer 
    CGPoint point = [[notification userInfo] valueForKey:@"sp"]; 


    // more code here.... 


} 

मैं चारों ओर से खुदाई कर रहा है फेंकता है, और इस समाधान मिल गया है, लेकिन मैं अभी भी कोई त्रुटि मिलती है।

-(void)setPosition:(CGPoint)point 
{ 
    // ERROR: Incompatile type for argument 1 of "Value With Point" 
    NSValue *pointAsObject = [NSValue valueWithPoint:point]; 
    NSDictionary *dict = [[NSDictionary alloc] 
         initWithObjectsAndKeys:@"sp", 
         pointAsObject, 
         nil]; 

    [[NSNotificationCenter defaultCenter] 
    postNotificationName:@"SpriteDidSetPosition" 
    object:self 
    userInfo:dict]; 

    [super setPosition:point]; 
} 

यह मुझे पागल कर रहा है। और मुझे आगे भी भ्रमित करने के लिए, इस

-(void)setPosition:(NSPoint)point 
{ 

    NSValue *pointAsObject = [NSValue valueWithPoint:point]; 
    NSDictionary *dict = [[NSDictionary alloc] init]; 
    [dict initWithObjectsAndKeys:@"sp", pointAsObject, nil]; 

    [[NSNotificationCenter defaultCenter] 
    postNotificationName:@"SpriteDidSetPosition" 
    object:self 
    userInfo:dict]; 

    [super setPosition:CGPointMake(point.x, point.y)]; 
} 

प्राप्त की त्रुटि से छुटकारा setPosition में तरह NSPoint को CGPoint बदल रहा है, लेकिन मैं अभी भी setViewPointCenter में खराब कर दिया हूँ। और जैसा कि मैं इसे समझता हूं, CGPoint और NSPoint एक ही चीज़ के बराबर होना चाहिए, लेकिन ऐसा लगता है कि वे ऐसा नहीं करते हैं।

क्या किसी के पास एक शब्दकोश में एक CGPoint को पास करने का एक उदाहरण है? मेरे द्वारा इसका निर्धारण नहीं किया जा सकता।

यह आईफोन के लिए है, जो कि एक फर्क पड़ता है।

उत्तर

13

+[NSValue valueWithCGPoint] का उपयोग करने का प्रयास करें।

+1

मेरी इच्छा है कि मैं इसे जल्द ही पाउंगा, लेकिन मैं भर में ठोकर खाई यह, जो इसे अधिक गहराई में समझाता है। http://stackoverflow.com/questions/1032726/nsnotification-userinfo-example – gargantuan

1

आप +numberWithFloat: का उपयोग कर सीजीपॉइंट से एनएसएनंबर ऑब्जेक्ट्स में एक्स और वाई मानों को समाहित कर सकते हैं और फिर दो परिणामी NSNumber ऑब्जेक्ट्स को शब्दकोश में जोड़ सकते हैं। इसके बाद आप का उपयोग कर दूसरी तरफ CGPoint फिर से संगठित कर सकते हैं:

CGPoint myPoint; 
myPoint.x = [myNumberObject floatValue]; 
myPoint.y = [myNumberObject2 floatValue]; 

कारण यह पहली कोशिश में काम नहीं किया था कि CGPoint एक वस्तु नहीं है, यह एक सी struct है।

0

मैंने GC और NSPoints पर पढ़ा नहीं है, लेकिन NSDictionary क्या डेटा-प्रकार हो सकता है? दस्तावेज़ों की जांच करें, शायद आपको इसे NSData पर डालना चाहिए।

6

मैं इसे NSStringFromCGPoint() function का उपयोग स्ट्रिंग में बदलने के लिए करता हूं, और उसके बाद इसे वापस करने के लिए CGPointFromString() function का उपयोग करता हूं।

+0

लिंक मेरे लिए टूटा हुआ है। नया लिंक जहां दोनों कार्य अब स्थित हैं: https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIKitFunctionReference/Reference/reference.html – cprcrack

0

@ बेन गॉटलिब ने उत्तर देने के बाद बहुत लंबा समय दिया है, उसका उत्तर अच्छी तरह से है, लेकिन भविष्य के लिए मैं संदर्भ के लिए एक उदाहरण रख रहा हूं।

// In example, I want to send CGPoint with notification 
[[NSNotificationCenter defaultCenter] postNotificationName:@"MyNotification" object:@{@"someKeyToHoldCGPoint":[NSValue valueWithCGPoint:CGPointMake(10, 10)]}]; 

- (void) getPoints:(NSNotification *)notification { 

    //get the dictionary object from notification 
    NSDictionary *p = (NSDictionary *)notification.object; 

    //get the NSValue object from dictionary p 
    NSValue *value = [p valueForKey:@"someKeyToHoldCGPoint"]; 

    //convert the value to CGPoint 
    CGPoint points = [value CGPointValue]; 

    //check if we've the correct value 
    NSLog(@"%@",NSStringFromCGPoint(points)); 
} 

इसे लॉग करना चाहिए, (10,10)