我创建了一个模型,我想保存一些我需要能够在其他两个类中访问的数据。我已经尝试在尝试添加之前和之后记录我的数组self
,但它之前和之后都是空的。我已经尝试了我能想到的一切,加上大量的谷歌搜索,但我无法让它发挥作用。这是我创建的用于将一些数据存储到的模型:
// 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
这是我试图从中访问 NSMutableArray 的类
// 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
// 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];
}
}
}
}
正如您在 if 块中看到的那样,如果数组未初始化,(!self.model.selectedLetters)
我试图通过添加对象来初始化数组。self
否则,我添加对象。我正在尝试使用objective-c,并且对这门语言仍然很陌生,所以我确信这个过程有一些简单的东西我不会完全理解。