addObjects到我的模型(NSObject类)NSMutableDictionary

时间:2014-03-06 21:03:10

标签: ios iphone objective-c nsmutablearray nsobject

我卡在某处。我想在我的模型类中创建NSMutableDictionary,其名称为Data.h& .m,我想从控制器中添加一些对象。我尝试了一些Methots,但我不能这样做..

我的模型.m文件

#import "Data.h"
//Dictiniory and Array Definitions...

NSMutableArray *plist;
NSMutableDictionary *kartlarDict;
@implementation Data

+(void)load{
    plist = [[NSMutableArray alloc] initWithObjects:@"Garanti", nil];
}

+(NSMutableArray *)getPlistArray {
    return plist;
}

//set Methots for kartlarDict...
-(id)initWithBankaAdi:(NSString *)aBankaAdi KartAdi:(NSString *)aKartAdi{`

        bankaFK = @"BankaAdi";
         kartFK  = @"KartAdi";

        bankaAdi = aBankaAdi;
        kartAdi  = aKartAdi;

        NSMutableDictionary *tmpKartlarDict = [[NSMutableDictionary alloc]     initWithObjectsAndKeys:
                                           bankaAdi,bankaFK,
                                           kartAdi,kartFK, nil];

         [kartlarDict addEntriesFromDictionary:tmpKartlarDict];

        return self;
}

//get Methots for kartlarDict...
+(NSMutableDictionary *)getKartlarDict{
    return kartlarDict;
}

@end

有我的.m CONTROLLER文件。

- (void)viewDidLoad
{
    [super viewDidLoad];

    [Data getKartlarDict];
    [Data getPlistArray];
     NSLog(@"%@",[Data getKartlarDict]);

     Data *p1 = [[Data alloc] initWithBankaAdi:@"HSBC" KartAdi:@"Advantage Kart"];

     kartlarListe = [[NSMutableArray alloc] initWithObjects:[Data getKartlarDict], nil];
}

我只想创建一些NSMutableDict或Array,我想从我的控制器文件中设置它们,当然我想让它们回到所有其他控制器文件。

我该怎么做?请帮忙 !

注意:我现在是新手,但这些天我总是睡5个小时因为我想学习这门语言a.s.a.p.我看过太多YouTube视频和苹果资源..

2 个答案:

答案 0 :(得分:0)

在初始化方法中kartlarDict未初始化:

-(id)initWithBankaAdi:(NSString *)aBankaAdi KartAdi:(NSString *)aKartAdi{`
    bankaFK = @"BankaAdi";
    kartFK  = @"KartAdi";
    bankaAdi = aBankaAdi;
    kartAdi  = aKartAdi;

    NSMutableDictionary *tmpKartlarDict = [[NSMutableDictionary alloc]     initWithObjectsAndKeys:
                                       bankaAdi,bankaFK,
                                       kartAdi,kartFK, nil];

    [kartlarDict addEntriesFromDictionary:tmpKartlarDict];

// kartlarDict is nil here!!!!!!!!!!!!!!
    return self;
}

变化

[kartlarDict addEntriesFromDictionary:tmpKartlarDict];

kartlarDict = [NSMutableDictionary dictionaryWithDictionary:tmpKartlarDict]

甚至

kartlarDict = [tmpKartlarDict mutableCopy]; // it will be enought

答案 1 :(得分:0)

试试这个,我已解释过:

    -(id)initWithBankaAdi:(NSString *)aBankaAdi KartAdi:(NSString *)aKartAdi{
        if(self = [self init]){ //First of all, you should call super init or self basic init method 
           //following code will be more clear.
           NSMutableDictionary *tmpKartlarDict = [[NSMutableDictionary alloc]     initWithObjectsAndKeys:
                                               aBankaAdi,@"BankaAdi",
                                               aKartAdi, @"KartAdi", nil];

           if(!kartlarDict){ //Maybe kartlarDict init in the above basic init method, if not, ignore the judgement.
               kartlarDict = [NSMutableDictionary dictionaryWithDictionary:tmpKartlarDict]
           }
           else{
              [kartlarDict addEntriesFromDictionary:tmpKartlarDict];

           }
        }
        return self;
    }