Objective-C如何使用NSArrays的令牌?

时间:2015-05-08 19:59:00

标签: objective-c token nslog

所以,我在Objective-C代码中使用OS,Xcode应用程序。这是我到目前为止所做的:

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[])
{
    @autoreleasepool {

        /// IN THIS CHALLENGE, I HAVE TO MAKE MY OWN PLIST
        /// IT HAS TO INCLUDE ALL 8 TYPES: ARRAY, DICTIONARY, STRING, DATA, DATE, INTEGER, FLOAT, AND BOOLEAN...

        // Add the elements
        NSMutableArray *array;
        NSMutableDictionary *dictionary;
        NSString *string;
        NSData *data;
        NSDate *date;
        int integer;
        float floating;
        BOOL boolean;


        // Set up the elements
        [array addObject:[NSString stringWithFormat:@"\"Array\""]];
        [dictionary setObject:@"\"Dictionary\""
                       forKey:@"dic"];
        string = [NSString stringWithFormat:@"\"String\""];
        data = [NSData dataWithContentsOfFile:@"/tmp/cool.text"];
        date = [NSDate date];
        integer = 50;
        floating = 50.5;
        boolean = YES;


        // Log them out
        NSLog(@"Array: %@\n\n", array);
        NSLog(@"Dictionary: %@\n\n", dictionary);
        NSLog(@"String: %@\n\n", string);
        NSLog(@"Data: %@\n\n", data);
        NSLog(@"Date: %@\n\n", date);
        NSLog(@"Integer: %d\n\n", integer);
        NSLog(@"Float: %f\n\n", floating);
        NSLog(@"Boolean: %hhd\n\n", boolean);

    }
    return 0;
}

那么,这个“挑战”(来自我正在遵循的编码书)应该做的是在控制台中打印出每个这些的价值。但是,我在这里遇到了问题。当我运行应用程序时,日志打印出来:

  

2015-05-08 14:48:23.588 CHALLENGE 7. Plist [21817:1995500]数组:   (空)

     

2015-05-08 14:48:23.590 CHALLENGE 7. Plist [21817:1995500]字典:   (空)

     

2015-05-08 14:48:23.590 CHALLENGE 7. Plist [21817:1995500]字符串:   “字符串”

     

2015-05-08 14:48:23.590 CHALLENGE 7. Plist [21817:1995500]数据:   &lt; 43687269 73746961 6e206973 20636f6f 6c210a43 68726973 7469616e   20697320 636f6f6c 210a4368 72697374 69616e20 69732063 6f6f6c21   0a436872 69737469 616e2069 7320636f 6f6c210a 43687269 73746961   6e206973 20636f6f 6c210a43 68726973 7469616e 20697320 636f6f6c   210a4368 72697374 69616e20 69732063 6f6f6c21 0a436872 69737469   616e2069 7320636f 6f6c210a 43687269 73746961 6e206973 20636f6f   6c210a43 68726973 7469616e 20697320 636f6f6c 21&gt;

     

2015-05-08 14:48:23.596挑战7. Plist [21817:1995500]日期:   2015-05-08 19:48:23 +0000

     

2015-05-08 14:48:23.596 CHALLENGE 7. Plist [21817:1995500]整数:50

     

2015-05-08 14:48:23.596 CHALLENGE 7. Plist [21817:1995500] Float:   50.500000

     

2015-05-08 14:48:23.597 CHALLENGE 7. Plist [21817:1995500]布尔:1

所以,这没关系,但我想了解如何使用NSLog()正确打印出值。如您所见,数组和字典都打印为(null),或者更确切地说,没有值。并且包含单词“Random Text”的文件中的数据不会打印出String值。它似乎打印出文件本身的非人类可读值。是因为我使用%@标记吗?如果是这样,应该我使用什么?

1 个答案:

答案 0 :(得分:1)

在向其中添加任何内容之前,您必须初始化arrays

NSMutableArray *array = [[NSMutableArray alloc] init];