-4

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

根(字典)(12 项)

compteur111 字符串 1 compteur222 字符串 2 compteur333 字符串 3 compteur444 字符串 4 compteur555 字符串 5 compteur666 字符串 6 name1 字符串 11 name2 字符串 12 name3 字符串 13 name4 字符串 14 name5 字符串 15 name6 字符串 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

这是我的崩溃日志:

2013-03-31 20:08:20.723 iCounterClick[28622:c07]   ! Error reading DataP.plist
2013-03-31 20:08:21.739 iCounterClick[28622:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSDictionary initWithObjects:forKeys:]: count of objects (0) differs from count of keys (1)'
*** First throw call stack:
(0x1c96012 0x10d3e7e 0x1ca1737 0x1cc28c2 0x2214 0x10e7705 0x1b2c0 0x257a64 0x10e7705 0x1b2c0 0x1b258 0xdc021 0xdc57f 0xdb6e8 0x4acef 0x4af02 0x28d4a 0x1a698 0x1bf1df9 0x1bf1ad0 0x1c0bbf5 0x1c0b962 0x1c3cbb6 0x1c3bf44 0x1c3be1b 0x1bf07e3 0x1bf0668 0x17ffc 0x1c9d 0x1bc5 0x1)
libc++abi.dylib: terminate called throwing an exception
(lldb) 
4

1 回答 1

1

我同意@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) {

在初始化程序中使用它们。

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

编辑

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

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

你收到什么日志?

于 2013-03-31T19:36:56.357 回答