我有一个plist:
<plist version="1.0">
<array>
<dict>
<key>name</key>
<string>Alabama</string>
<key>abreviation</key>
<string>AL</string>
<key>date</key>
<string>1819</string>
<key>population</key>
<string>4,627,851</string>
<key>capital</key>
<string>Montgomery</string>
<key>largestCity</key>
<string>Birmingham</string>
</dict>
<dict>
<key>name</key>
<string>Alaska</string>
<key>abreviation</key>
<string>AK</string>
<key>date</key>
<string>1959</string>
<key>population</key>
<string>683,478</string>
<key>capital</key>
<string>Juneau</string>
<key>largestCity</key>
<string>Anchorage</string>
</dict>
...
</array>
</plist>
Run Code Online (Sandbox Code Playgroud)
我试图将它加载到像这样的NSDictionary:
NSString *path = [[NSBundle mainBundle] pathForResource:@"stateInfo" ofType:@"plist"];
NSFileManager *fileManager = [NSFileManager defaultManager];
if ([fileManager fileExistsAtPath:path]) {
NSLog(@"The file exists");
} else {
NSLog(@"The file does not exist");
}
NSMutableDictionary *myDic = [[NSMutableDictionary alloc] initWithContentsOfFile:path];
//NSDictionary *myDic = [NSDictionary dictionaryWithContentsOfFile:path];
NSLog(@"The count: %i", [myDic count]); …Run Code Online (Sandbox Code Playgroud) 我有一个NSAd的NSDate对象,我想对它们进行排序,以便今天是0,昨天是1等.
它是升序还是降序,我使用函数,选择器还是什么?
对于我的应用程序,我试图将CGRect对象存储到NSMutableArray.它正在加载并在日志语句中打印,但尝试CGRect从数组中取出s会显示错误.这是一段代码:
CGRect lineRact = CGRectMake([[attributeDict objectForKey:@"x"] floatValue],
[[attributeDict objectForKey:@"y"] floatValue],
[[attributeDict objectForKey:@"width"] floatValue],
[[attributeDict objectForKey:@"height"] floatValue]);
[lineRactangle addObject:NSStringFromCGRect(lineRact)];
Run Code Online (Sandbox Code Playgroud)
如何从阵列中取回rects?
我有一个充满CGRects的数组.那部分没问题.问题是当我从数组中检索CGRects时,我得到了奇怪的错误.看看我的代码.
NSArray *frameLocations = [NSArray arrayWithObjects:
[NSValue valueWithCGRect:CGRectMake(20, 20, 121, 124)],
[NSValue valueWithCGRect:CGRectMake(176, 20, 121, 124)],
nil];
Run Code Online (Sandbox Code Playgroud)
比我在for循环中得到帧,像这样:
CGRect *imageFrame = [[frameLocations objectAtIndex:i] CGRectValue];
Run Code Online (Sandbox Code Playgroud)
我尝试了一系列不同的变体,将这一行传播到多个变量上.但我似乎无法得到它.
NSArray中各种排序方法使用的排序算法是否稳定?(因为它们是"稳定排序"算法,其中具有相同排序键的项目保留了相对顺序.)
所以我收到了这个警告:
Semantic Issue: Incompatible pointer to integer conversion sending 'NSUInteger *' (aka 'unsigned int *') to parameter of type 'NSUInteger' (aka 'unsigned int')
Run Code Online (Sandbox Code Playgroud)
基本上我正在引入一个JSON提要..循环遍历它以匹配列与数据,然后将数据放在一个对象中用于表行...
NSDictionary *js_result = [response JSONValue];
NSLog(@"This is the LIST: %@",[js_result objectForKey:@"LIST"]);
// get columns
NSArray *columns = [[js_result objectForKey:@"LIST"] componentsSeparatedByString:@","];
// get data
NSArray *rows = [[js_result objectForKey:@"QUERY"] objectForKey:@"DATA"];
NSUInteger *study_id_int = (NSUInteger *)[columns indexOfObject:@"STUDY_ID_DICOM"];
NSUInteger *study_desc_int = (NSUInteger *)[columns indexOfObject:@"STUDY_DESCRIPTION"];
NSUInteger *study_date_int = (NSUInteger *)[columns indexOfObject:@"STUDY_DATETIME"];
NSUInteger *modality_int = (NSUInteger *)[columns indexOfObject:@"MODALITY"];
NSUInteger …Run Code Online (Sandbox Code Playgroud) 我想将BOOL传递给[NSArray makeObjectsPerformSelector:withObject:]参数.例如
[buttons makeObjectsPerformSelector:@selector(setEnabled:) withObject: NO];
Run Code Online (Sandbox Code Playgroud)
上面的代码不起作用,因为withObject只接受id.
什么是正确的方法呢?
我看到了一些代码:
[buttons makeObjectsPerformSelector:@selector(setEnabled:) withObject: (id)kCFBooleanTrue];
[buttons makeObjectsPerformSelector:@selector(setEnabled:) withObject: (id)kCFBooleanFalse];
Run Code Online (Sandbox Code Playgroud)
这在4.2模拟器上工作正常但在4.2 iphone上失败.
我有两个UIColor实例,并希望使用它们创建一个渐变.代码有效,但是当我调用arrayWithObject:构造函数时它会给我一个警告:
警告:语义问题:不兼容的指针类型将'CGColorRef'(又名'struct CGColor*')发送到'id'类型的参数
我怀疑那里潜藏着与警告相关的其他问题(例如泄漏).这是片段:
UIColor *startColor, *endColor;
// ...
NSArray *colors = [NSArray arrayWithObjects:
startColor.CGColor, endColor.CGColor, nil];
CGGradientRef gradient = CGGradientCreateWithColors(colorSpace,
(CFArrayRef) colors, NULL);
Run Code Online (Sandbox Code Playgroud)
如何重写代码以安全的方式摆脱这个警告?
CGFloat components[]我有一个NSArray由NSNumbers,我想这个转换成NSArray的NSStrings,通过获取stringValue每个NSNumber第一阵列英寸
我想到的方法是迭代第一个中的每个值,获取其字符串值并将其添加到另一个数组中.但是应该有一个更优雅的解决方案.你知道吗?
让我们假设我有一个像这样的数组
NSArray* arr = @[@"1",@"4",@"2",@"8",@"11",@"10",@"14",@"9"]; //note: strings containing numbers
Run Code Online (Sandbox Code Playgroud)
我想这样排序:[1,2,4,8,9,10,11,14]
但是,如果我使用
[arr sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
Run Code Online (Sandbox Code Playgroud)
我得到[1,10,11,14,2,4,8,9] ......如果我使用:
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"length" ascending:YES];
NSArray *sortDescriptors = @[sortDescriptor];
[arr sortedArrayUsingDescriptors:sortDescriptors];
Run Code Online (Sandbox Code Playgroud)
我得到这样的东西:[1,4,2,8,9,11,10,14]
我如何组合两个谓词?或者是解决这个问题的其他更简单的方法吗?注意:此数组的输出仅用于调试目的,我不在乎结果是否将数组转换为整数,只要我可以在控制台中使用NSLog打印谢谢