Objective c 读取plist文件时出错

Objective c 读取plist文件时出错,objective-c,plist,Objective C,Plist,当我在Xcode中添加属性列表时,当我们按下按钮时,我的应用程序崩溃。我搜索了很多次,但没有发现错误;请帮帮我。这是我的viewController.m,但没有零变量。。。所以这不是问题所在。 这是我的plist文件: 词根(词汇)(12项) 编译器111字符串1 Computer222字符串2 Computer333字符串3 Computer444字符串4 编译器555字符串5 计算机666字符串6 名称1字符串11 名称2字符串12 名称3字符串13 名称4字符串14 名称5字符串15 名称

当我在Xcode中添加属性列表时,当我们按下按钮时,我的应用程序崩溃。我搜索了很多次,但没有发现错误;请帮帮我。这是我的viewController.m,但没有零变量。。。所以这不是问题所在。 这是我的plist文件:

词根(词汇)(12项)

编译器111字符串1 Computer222字符串2 Computer333字符串3 Computer444字符串4 编译器555字符串5 计算机666字符串6 名称1字符串11 名称2字符串12 名称3字符串13 名称4字符串14 名称5字符串15 名称6字符串16

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

//Synthétisation des propriétés
@synthesize labelValeurCompteur1;
@synthesize labelValeurCompteur2;
@synthesize labelValeurCompteur3;
@synthesize labelValeurCompteur4;
@synthesize labelValeurCompteur5;
@synthesize labelValeurCompteur6;

@synthesize nameCompteur1;
@synthesize nameCompteur2;
@synthesize nameCompteur3;
@synthesize nameCompteur4;
@synthesize nameCompteur5;
@synthesize nameCompteur6;

@synthesize compteur11;
@synthesize compteur22;
@synthesize compteur33;
@synthesize compteur44;
@synthesize compteur55;
@synthesize compteur66;

@synthesize compteur01;
@synthesize compteur02;
@synthesize compteur03;
@synthesize compteur04;
@synthesize compteur05;
@synthesize compteur06;

-(IBAction)backgroundTouched:(id)sender
{
    [labelValeurCompteur1 resignFirstResponder];
    [labelValeurCompteur2 resignFirstResponder];
    [labelValeurCompteur3 resignFirstResponder];
    [labelValeurCompteur4 resignFirstResponder];
    [labelValeurCompteur5 resignFirstResponder];
    [labelValeurCompteur6 resignFirstResponder];

    [nameCompteur1 resignFirstResponder];
    [nameCompteur2 resignFirstResponder];
    [nameCompteur3 resignFirstResponder];
    [nameCompteur4 resignFirstResponder];
    [nameCompteur5 resignFirstResponder];
    [nameCompteur6 resignFirstResponder];    
}   

-(IBAction)numeroCompteur:(id)sender
{
    NSLog(@"> numeroCompteur");

    [labelValeurCompteur1 resignFirstResponder];
    [labelValeurCompteur2 resignFirstResponder];
    [labelValeurCompteur3 resignFirstResponder];
    [labelValeurCompteur4 resignFirstResponder];
    [labelValeurCompteur5 resignFirstResponder];
    [labelValeurCompteur6 resignFirstResponder];

    [nameCompteur1 resignFirstResponder];
    [nameCompteur2 resignFirstResponder];
    [nameCompteur3 resignFirstResponder];
    [nameCompteur4 resignFirstResponder];
    [nameCompteur5 resignFirstResponder];
    [nameCompteur6 resignFirstResponder];  


int variableDebutCompteur1 = [[labelValeurCompteur1 text] intValue];
int variableDebutCompteur2 = [[labelValeurCompteur2 text] intValue];
int variableDebutCompteur3 = [[labelValeurCompteur3 text] intValue];
int variableDebutCompteur4 = [[labelValeurCompteur4 text] intValue];
int variableDebutCompteur5 = [[labelValeurCompteur5 text] intValue];
int variableDebutCompteur6 = [[labelValeurCompteur6 text] intValue];    

compteur1.value = variableDebutCompteur1;
compteur2.value = variableDebutCompteur2;
compteur3.value = variableDebutCompteur3;
compteur4.value = variableDebutCompteur4;
compteur5.value = variableDebutCompteur5;
compteur6.value = variableDebutCompteur6;

labelValeurCompteur1.text = [NSString stringWithFormat: @"%d", variableDebutCompteur1];
labelValeurCompteur2.text = [NSString stringWithFormat: @"%d", variableDebutCompteur2];
labelValeurCompteur3.text = [NSString stringWithFormat: @"%d", variableDebutCompteur3];
labelValeurCompteur4.text = [NSString stringWithFormat: @"%d", variableDebutCompteur4];
labelValeurCompteur5.text = [NSString stringWithFormat: @"%d", variableDebutCompteur5];
labelValeurCompteur6.text = [NSString stringWithFormat: @"%d", variableDebutCompteur6];

compteur11 = labelValeurCompteur1.text;
compteur22 = labelValeurCompteur2.text;
compteur33 = labelValeurCompteur3.text;
compteur44 = labelValeurCompteur4.text;
compteur55 = labelValeurCompteur5.text;
compteur66 = labelValeurCompteur6.text;    

{
    NSLog(@" - Writing DataP.plist Counters");
    NSString *error;
    NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString *plistPath = [rootPath stringByAppendingPathComponent:@"DataP.plist"];
    NSDictionary *plistDict = [NSDictionary dictionaryWithObjects:
                               [NSArray arrayWithObjects: compteur11, compteur22, compteur33, compteur44, compteur55, compteur66, nil]
                                                          forKeys:[NSArray arrayWithObjects: @"compteur111", @"compteur222", @"compteur333", @"compteur444", @"compteur555", @"compteur666", nil]];
    NSData *plistData = [NSPropertyListSerialization dataFromPropertyList:plistDict
                                                                   format:NSPropertyListXMLFormat_v1_0
                                                         errorDescription:&error];
    if(plistData) {
        [plistData writeToFile:plistPath atomically:YES];
    }
    else {
        [error release];
    }        
}

nameCompteur1.text = nameCompteur1.text;
nameCompteur2.text = nameCompteur2.text;
nameCompteur3.text = nameCompteur3.text;
nameCompteur4.text = nameCompteur4.text;
nameCompteur5.text = nameCompteur5.text;
nameCompteur6.text = nameCompteur6.text;

{
    NSLog(@" - Writing DataP.plist Labels");
    NSString *error;
    NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];

    NSString *plistPath = [rootPath stringByAppendingPathComponent:@"DataP.plist"];
    NSDictionary *plistDict = [NSDictionary dictionaryWithObjects:
                               [NSArray arrayWithObjects: compteur01, compteur02, compteur03, compteur04, compteur05, compteur06, nil]
                                                          forKeys:[NSArray arrayWithObjects: @"name1", @"name2", @"name3", @"name4", @"name5", @"name6", nil]];
    NSData *plistData = [NSPropertyListSerialization dataFromPropertyList:plistDict
                                                                   format:NSPropertyListXMLFormat_v1_0
                                                         errorDescription:&error];
    if(plistData) {
        [plistData writeToFile:plistPath atomically:YES];
    }
    else {
        NSLog(@"  ! Error writeToFile:plistData:labels");
        [error release];
    }
}

NSLog(@"< numeroCompteur");
}

-(IBAction)changerValeurCompteurUn:(UIStepper *)sender
{
    int variableDebutCompteur1 = [sender value];
    [labelValeurCompteur1 setText:[NSString stringWithFormat:@"%d", (int)variableDebutCompteur1]];
}
-(IBAction)changerValeurCompteurDeux:(UIStepper *)sender
{
    int valeurCompteur2 = [sender value];
    [labelValeurCompteur2 setText:[NSString stringWithFormat:@"%d", (int)valeurCompteur2]];
}
-(IBAction)changerValeurCompteurTrois:(UIStepper *)sender
{
    int valeurCompteur3 = [sender value];
    [labelValeurCompteur3 setText:[NSString stringWithFormat:@"%d", (int)valeurCompteur3]];
}
-(IBAction)changerValeurCompteurQuatre:(UIStepper *)sender
{
    int valeurCompteur4 = [sender value];
    [labelValeurCompteur4 setText:[NSString stringWithFormat:@"%d", (int)valeurCompteur4]];
}
-(IBAction)changerValeurCompteurCinq:(UIStepper *)sender
{
    int valeurCompteur5 = [sender value];
    [labelValeurCompteur5 setText:[NSString stringWithFormat:@"%d", (int)valeurCompteur5]];
}
-(IBAction)changerValeurCompteurSix:(UIStepper *)sender
{
    int valeurCompteur6 = [sender value];
    [labelValeurCompteur6 setText:[NSString stringWithFormat:@"%d", (int)valeurCompteur6]];
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    [self.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"Bois.JPG"]]];


    // Do any additional setup after loading the view, typically from a nib.
    self = [super init];
    NSLog(@"> viewDidLoad");
        if (self) {
            NSString *errorDesc = nil;
            NSPropertyListFormat format;
            NSString *plistPath;
            NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                                      NSUserDomainMask, YES) objectAtIndex:0];
            plistPath = [rootPath stringByAppendingPathComponent:@"DataP.plist"];
            if (![[NSFileManager defaultManager] fileExistsAtPath:plistPath]) {
                plistPath = [[NSBundle mainBundle] pathForResource:@"DataP" ofType:@"plist"];
            }
            else
                NSLog(@"  ! Error reading DataP.plist");
            NSData *plistXML = [[NSFileManager defaultManager] contentsAtPath:plistPath];
            NSDictionary *tempR = (NSDictionary *)[NSPropertyListSerialization
                                                  propertyListFromData:plistXML
                                                  mutabilityOption:NSPropertyListMutableContainersAndLeaves
                                                  format:&format
                                                  errorDescription:&errorDesc];
            if (!tempR) {
                NSLog(@"  ! Error reading plist: %@, format: %d", errorDesc, format);
            }
            self.compteur11 = [tempR objectForKey:@"compteur111"];
            labelValeurCompteur1.text = compteur11;
            self.compteur22 = [tempR objectForKey:@"compteur222"];
            labelValeurCompteur2.text = compteur22;
            self.compteur33 = [tempR objectForKey:@"compteur333"];
            labelValeurCompteur3.text = compteur33;
            self.compteur44 = [tempR objectForKey:@"compteur444"];
            labelValeurCompteur4.text = compteur44;
            self.compteur55 = [tempR objectForKey:@"compteur555"];
            labelValeurCompteur5.text = compteur55;
            self.compteur66 = [tempR objectForKey:@"compteur666"];
            labelValeurCompteur6.text = compteur66;

             self.compteur01 = [tempR objectForKey:@"name1"];
             nameCompteur1.text = compteur01;
             self.compteur02 = [tempR objectForKey:@"name2"];
             nameCompteur2.text = compteur02;
             self.compteur03 = [tempR objectForKey:@"name3"];
             nameCompteur3.text = compteur03;
             self.compteur04 = [tempR objectForKey:@"name4"];
             nameCompteur4.text = compteur04;
             self.compteur05 = [tempR objectForKey:@"name5"];
             nameCompteur5.text = compteur05;
             self.compteur06 = [tempR objectForKey:@"name6"];
             nameCompteur6.text = compteur06;
        }
    NSLog(@"< viewDidLoad");
}    

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

我同意JoshCaswell的观点。根据你的问题,很难理解发生了什么

通常类似的错误是由
nil
值引起的。类似错误的示例可以从以下代码段中派生

[NSDictionary dictionaryWithObjects: [NSArray arrayWithObjects: myObject, nil] forKeys:[NSArray arrayWithObjects: @"myObjectKey", nil]];
如果
myObject
nil
,则燃烧!您将收到一个异常,表示对象数组的大小为0,而键数组的大小为1

我的建议

尝试删除一些代码,并向我们显示要加载的plist文件

我的笔记

快速阅读你的代码,我发现了一些奇怪的东西。特别是,为什么在
viewDidLoad
方法中使用这些行

self = [super init];
NSLog(@"> viewDidLoad");
    if (self) {
在初始值设定项中使用它们

viewdiload
方法具有特定含义:您可以确保与控制器关联的
视图已正确加载到内存中,并且可以执行其他自定义

编辑

如前所述,不能在字典中插入
nil
obects。一个简单的测试如下:

if(compteur11) {
    NSLog(@"compteur11 is %@");
} else {
    NSLog(@"compteur11 is nil");
}

您收到了什么日志?

您要求其他人阅读和调试的代码太多了。您自己采取了哪些措施来缩小问题的根源?异常消息为您提供了大量信息。你用它做了什么?把你的问题写得最少,并展示给别人看。谢谢提醒我,下面是代码:NSString*error;NSString*rootPath=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES)对象索引:0];NSString*plistPath=[rootPath stringByAppendingPathComponent:@“DataP.plist”];NSDictionary*plistDict=[NSDictionary Dictionary WithObjects:[NSArray arrayWithObjects:@user2187565请编辑您的问题并将代码放在那里。提前谢谢。COMPTUR11、COMPTUR22、COMPTUR33、COMPTUR44、COMPTUR55、COMPTUR66、COMPTUR01、COMPTUR02、COMPTUR03、COMPTUR04、COMPTUR05、COMPTUR06、nil]福克斯:[N阵列阵列,其对象为:@“COMPTUR111”、“COMPTUR222”、“COMPTUR333”、“COMPTUR444”、“COMPTUR555”、“COMPTUR666”、“name1”、“name2”、“name3”、“name4”、“name5”、“name6”、nil]];谢谢你的回答,但我是新来的,我必须等5个小时才能发布另一个问题,所以我发布了带有代码的评论,对不起。只需编辑你创建的问题。我认为你可以做到。总之,其中一个对象
compteur11,compteur22,compteur33,compteur44,compteur55,compteur66,compteur01,compteur02,compteur03,compteur04,compteur05,compteur06
nil
。这就是问题所在。
if(compteur11) {
    NSLog(@"compteur11 is %@");
} else {
    NSLog(@"compteur11 is nil");
}