So, I'm in a OS, Xcode application, in Objective-C code. Here's what I have so far:
所以,我在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;
}
So, what this "challenge" (from a coding book I'm following) is supposed to do, is print out the values of each of these in the console. But, I have a problem here. When I run the application, the log prints out this:
那么,这个“挑战”(来自我正在遵循的编码书)应该做的是在控制台中打印出每个这些的价值。但是,我在这里遇到了问题。当我运行应用程序时,日志打印出来:
2015-05-08 14:48:23.588 CHALLENGE 7. Plist[21817:1995500] Array: (null)
2015-05-08 14:48:23.588挑战7. Plist [21817:1995500]数组:(null)
2015-05-08 14:48:23.590 CHALLENGE 7. Plist[21817:1995500] Dictionary: (null)
2015-05-08 14:48:23.590 CHALLENGE 7. Plist [21817:1995500]字典:(null)
2015-05-08 14:48:23.590 CHALLENGE 7. Plist[21817:1995500] String: "String"
2015-05-08 14:48:23.590 CHALLENGE 7. Plist [21817:1995500]字符串:“String”
2015-05-08 14:48:23.590 CHALLENGE 7. Plist[21817:1995500] Data: <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>
2015-05-07 14:48:23.590 CHALLENGE 7.的plist [21817:1995500]数据:<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>
2015-05-08 14:48:23.596 CHALLENGE 7. Plist[21817:1995500] Date: 2015-05-08 19:48:23 +0000
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] Integer: 50
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.596挑战7. Plist [21817:1995500] Float:50.500000
2015-05-08 14:48:23.597 CHALLENGE 7. Plist[21817:1995500] Boolean: 1
2015-05-08 14:48:23.597 CHALLENGE 7. Plist [21817:1995500]布尔值:1
So, this is OK, but I'd like some help on how to use the NSLog() to print out the values correctly. As you can see, both the array and the dictionary are printed out as (null), or rather, with no value. And the data from a file containing the words "Random Text" doesn't print out the String value. It seems to print out the non-human-readable value of the file itself. Is it because I used the %@ token with these? If so, what should I use?
所以,这没关系,但是我想要一些关于如何使用NSLog()正确打印出值的帮助。如您所见,数组和字典都打印为(null),或者更确切地说,没有值。并且包含单词“Random Text”的文件中的数据不会打印出String值。它似乎打印出文件本身的非人类可读值。是因为我使用%@标记吗?如果是这样,我应该使用什么?
1 个解决方案
#1
You have to initialize the arrays
before you add anything to it.
在向阵列添加任何内容之前,必须先初始化阵列。
NSMutableArray *array = [[NSMutableArray alloc] init];
#1
You have to initialize the arrays
before you add anything to it.
在向阵列添加任何内容之前,必须先初始化阵列。
NSMutableArray *array = [[NSMutableArray alloc] init];