2013-10-30 59 views
0

我創建了一個模型,我想保存一些我需要能夠在另外兩個類中訪問的數據。 我試過在嘗試向它添加self之前和之後嘗試記錄我的數組,但它在前後都是空的。我試過所有我能想到的,加上大量的谷歌搜索,但我無法得到這個工作。 這裏是我的模型,我創建了一些數據存儲到:如何在另一個類中使用一個類的NSMutablearray?

// CCModel.h

#import <Foundation/Foundation.h> 
@interface CCModel : NSObject 
// this is the array that I want to store my SKLetterNodes in 
@property(strong, nonatomic) NSMutableArray* selectedLetters; 
@end 

// CCModel.m

#import "CCModel.h" 

@implementation CCModel 
- (id)init 
{ 
    self = [super init]; 
    if (self) { 
     // not really to sure if this is the right approach to init the array that I'm going to need 
     self.selectedLetters = [NSMutableArray init]; 
    } 
    return self; 
} 
@end 

這裏有我想要的類從

// SKLetterNode.h

#import <SpriteKit/SpriteKit.h> 
#import "CCModel.h" 

@class SKLetterNode; 

@protocol LetterDragDelegateProtocol <NSObject> 
-(void)letterNode:(SKLetterNode*)letterNode didDragToPoint:(CGPoint)pt; 
@end 

@protocol LetterWasTouchedDelegateProtocol <NSObject> 
-(void) touchedPoint:(CGPoint)touchedPoint; 
@end 

@interface SKLetterNode : SKSpriteNode 
// Here is where I'm creating a property to access my model's NSMutableArray 
@property(strong, nonatomic) CCModel* model; 

.... 
@end 
0123訪問的NSMutableArray

// SKLetterNode.m - 我就包括相關的方法,因爲一切在這個類

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 

    if (!self.isSelected) { 

     SKLetterNode* lastLetter = [self.model.selectedLetters lastObject]; 

     if (lastLetter.bottomOrTop != self.bottomOrTop || self.model.selectedLetters.count == 0) { 

      CGSize expandSize = CGSizeMake(self.size.width * expandFactor, self.size.height * expandFactor); 

      SKAction* sound = [SKAction playSoundFileNamed:@"button.wav" waitForCompletion:NO]; 
      [self runAction:sound]; 

      self.isSelected = YES; 
      self.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:expandSize]; 
      self.physicsBody.affectedByGravity = NO; 
      self.physicsBody.allowsRotation = NO; 

      if (!self.model.selectedLetters) { 
       // Here is where I'm trying to init my array by adding an object 
       [self.model.selectedLetters arrayByAddingObject:self]; 
      } else { 
       // The array must already be initialized, so add the object 
       [self.model.selectedLetters addObject:self]; 
      } 


     } 

    } 
} 

正如你可以看到在工作中,如果(!self.model.selectedLetters)塊,我試圖給init陣列如果數組未初始化,則添加對象self。否則,我添加該對象。我正在努力學習Objective-C,而且對這門語言仍然很陌生,所以我確信這個過程只是簡單的一件事情,我不會放棄理解。

+1

你在哪裏實例化'model'?如果'''self.model.selectedLetters''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''',不管'selectedLetters'是否被初始化,並且'model'沒有被實例化,一個'模型'的屬性。 – nhgrif

回答

1

其他2張海報指出你的代碼有2個問題。 (不是實例化一個CCModel對象,並調用[NSMutableArray的初始化]而不是[[NSMutableArray裏的alloc]初始化]

的第三個問題與您的代碼是這一行:

[self.model.selectedLetters arrayByAddingObject:self]; 

無厘頭。這行代碼創建了一個新的不可變的數組,你再落到了地板上。在修復其他2個問題,你想用這樣的代碼

[self.model.selectedLetters addObject: self]; 

而且你真的要添加「自「到數組?(SKLetterNode對象)

+0

我的想法是,我需要在數組中存儲所有選中(觸摸)的SkLetterNodes,它們實際上只是SKSpriteNodes,因此每次調用touchesMoved時,都可以遍歷該數組並在所有SKLetterNodes上執行位置更改存儲在selectedLetter數組中。我還需要檢查每次touchesBegan被調用時,數組中的最後一個項目駐留在發生觸摸的相反行中。基本上,我有兩行字母,當拖動一個字母時,我希望所有選定的平行字母也移動。 – Scott

1
self.selectedLetters = [NSMutableArray init]; 

應該是:

self.selectedLetters = [[NSMutableArray alloc] init]; 

而你需要的地方alloc initCCModelSKLetterNode課呢。如果你不這樣做,selectedLetters上的alloc init永遠不會被調用,等等。

0

如果您嘗試使用MVC,則不應將視圖對象放入模型中。有一個單獨的模型對象代表你的數據模型。

另外MVC相關,你不應該從視圖中引用你的模型。使用你的控制器(可能是SK應用程序中的場景)將模型連接到視圖。內存管理:如果我正確地假設「模型」是每個視圖共享並在視圖之外創建的東西:模型保留視圖,視圖保留模型。這會像篩子一樣泄漏。連接不應該在所有的存在,但如果你想保持它,從視圖模型中的連接應該是弱:

@property(weak, nonatomic) CCModel* model; 

鄧肯指出,arrayByAddingObjects沒有意義存在。 [NSMutableArray init]甚至不應該編譯:總是修復你的警告,它們幾乎總是在你的代碼中指示錯誤。

另請注意:如果self.model.selectedLetters爲零,則問題可能是self.model爲零,而不是該model.selectedLetters爲零!

+0

因此,當在我的SKLetterNode中調用touchesBegan時,如果我想正確使用MVC模式,我的SKLetterNode與我的模型進行通信的最佳選擇是設置委託,並讓我的SKLetterNode與我的SKScene進行通信,並且然後讓SKSCene與模態進行交流?我也剛剛閱讀有關協議,似乎也可能是一種選擇。 – Scott

相關問題