我正在使用IPhone SDK,对于objective-c我是一个新手.现在我正在使用NSUserDefaults来保存和恢复我的iPhone应用程序上的设置.为了保存已创建的类,我将它们编码为字典形式,然后保存NSdictionary.
我的问题是我找不到合理的方法以合理的方式在字典中存储非值(IE是一个nil的类变量).更具体地说,我说我有一个"狗"类,它有NSString*尾色.让我们说我试图保存没有尾巴的狗的类实例,因此该实例的尾部颜色为零.拯救狗作为字典的合理方法是什么?它不会让我将nil保存到NSdictionary中.@""不好,因为如果我这样做(@""),@""是真的.我希望它像假的那样是假的.
我希望我的问题有道理,谢谢你的帮助!
我有一个NSMutableArray,里面有30个字典.每个都包含名称和值.我目前已经对名称进行了排序,以便按字母顺序在表格视图中显示.但是,我想制作一个UIButton来提供仍然显示名称的选项,但是按照价值排序.不需要显示该值.另外,在.m/.h文件中放置这些代码的位置是什么?谢谢!
sorting objective-c nsdictionary nsmutablearray iphone-sdk-3.0
我有一个NSDictionary.它有钥匙和物品.
为简单起见,键是问号,对象是计算答案分数.
现在,我之前是如何做到的,我将答案分数设置为键,将问号设置为对象.这样我就可以从字典中获取allKeys的数组,对它进行排序然后执行类似的操作:
for(NSString *string in tempArray){
NSLog(@"%@",[dictionary objectForKey:string]);
}
Run Code Online (Sandbox Code Playgroud)
然而,我现在遇到的(愚蠢的 - 我)问题是(显然...... duuhhh)键需要唯一,因此当计算出的答案分数相同时,只输出一个答案!
我需要一个解决方案.在PHP中你可以使用multisort
数组.我想知道在objective-c中是否有类似的解决方案,或者确实有人有更好的答案?
这里的任何帮助将不胜感激.
谢谢.
我正在使用NSDictionary来存储设置值,我正在尝试将它们分配给double.但是我不知道该怎么做,valueforkey返回一个指针id.我尝试使用'*'获取实际值,但这不起作用.
这是代码
tempBrain.operand = [variables valueForKey:[(NSString*)obj stringByReplacingOccurencesOfString:VARIABLE_PREFIX withString:@""]];
Run Code Online (Sandbox Code Playgroud)
tempBrain.operand是double变量的setter方法.变量是我的NSDictionary.
是否可以使用NSDictionary从发送的推送通知中检索信息?(例如,获取警报有效负载包含的标题,消息和声音).
我还想在有效负载中发送信息(例如字符串),供应用程序使用与标题或消息无关的信息.再说一遍,这可能吗?
我有一个Mutable NSArray存储对象类型(ids - NSNumber,NSString),它们将存储数字(1,2,3,4等),操作(+, - ,/,*等)和变量( x,y,z等).我将变量和相关值存储在NSDictionary中,键的NSStrings等于x,y,z,NSNumber值分别为5,5,2.我想将我的NSArray中的变量替换为存储在我的NSDictionary中的实际值.当我尝试替换对象时,我不断收到以下错误.请帮忙.
- [__ NSArrayI replaceObjectAtIndex:withObject:]:发送到实例的无法识别的选择器
NSDictionary *dict = [[NSDictionary alloc] initWithObjectsAndKeys:
[NSNumber numberWithDouble:5],@"x",
[NSNumber numberWithDouble:5],@"y",
[NSNumber numberWithDouble:2],@"z",
nil];
+ (double)runProgram:(id)program
usingVariableValues:(NSDictionary *)variableValues;
{
// introspection - ensure program is NSArray and variableValues is NSDictionary
if ([program isKindOfClass:[NSArray class]] && [variableValues isKindOfClass: [NSDictionary class]])
{
// array for program stack
NSMutableArray *stack = [program copy];
// Loop to replace variables with actual values in NSDictionary
for (NSUInteger i = 0; i < [stack count]; i++) …
Run Code Online (Sandbox Code Playgroud) 我有一个自定义类,我在字典中使用了键的实例.问题是,有时要求从字典中的值与我的自定义实例键回报的一个(不是所有的时间),nil
当钥匙确实是在字典中.我浏览了一下如何使用自定义对象作为字典键,我所能找到的就是你需要实现NSCopying
我已经完成的协议.
我的班级看起来像这样:
// .h
@interface MyObject: NSObject <NSCopying>
@property (copy) NSString* someString;
@property (copy) NSString* otherString;
@property int someInt;
+ (instancetype) objectWithString:(NSString*)str;
- (id) copyWithZone:(NSZone*)zone;
@end
// .m
@implementation MyObject
@synthesize someString;
@synthesize otherString;
@synthesize someInt;
+ (instancetype) objectWithString:(NSString*)str {
id obj = [[self alloc] init];
[obj setSomeString:str];
[obj setOtherString:[str lowercaseString];
[obj setSomeInt:0];
return obj;
}
- (id) copyWithZone:(NSZone*)zone {
id other = [[[self class] allocWithZone:zone] init];
[other setSomeString:self.someString];
[other setOtherString:self.otherString];
[other setSomeInt:self.someInt];
return …
Run Code Online (Sandbox Code Playgroud) 我有一个NSDictionary,我想按日期分组创建一个对象数组
主要NSDictionary的示例:
({
date = "2014-04-27";
group = "yellow.png";
length = 180;
},
{
date = "2014-04-28";
group = "blue.png";
length = 180;
},
{
date = "2014-04-27";
group = "blue.png";
length = 120;
})
Run Code Online (Sandbox Code Playgroud)
我想分组类似的东西:
2014-04-27 = (
{
date = "2014-04-27";
group = "yellow.png";
length = 180;
},
{
date = "2014-04-27";
group = "blue.png";
length = 180;
})
2014-04-28 = ( {
date = "2014-04-28";
group = "blue.png";
length = 120;
})
Run Code Online (Sandbox Code Playgroud)
有人能帮助我吗?我尝试了很多FOR,但我无法得到它
我有三个NSArray
,我想将它们全部合并为一个NSDictionary
.问题是当我遍历数组并创建字典时,它会覆盖前一个对象.最后我在字典中只有一个对象.我究竟做错了什么?这是我的代码:
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
for(int i=0; i<[array0 count]; i++) {
[dict setObject:[array0 objectAtIndex:i]
forKey:@"one"];
[dict setObject:[array1 objectAtIndex:i] f
orKey:@"two"];
[dict setObject:[array2 objectAtIndex:i]
forKey:@"three"];
}
Run Code Online (Sandbox Code Playgroud)
也许这会澄清我的意思......这是我想要的结果:
{one = array0_obj0, two = array1_obj0, three = array2_obj0},
{one = array0_obj1, two = array1_obj1, three = array2_obj1},
{one = array0_obj2, two = array1_obj2, three = array2_obj2},
etc
Run Code Online (Sandbox Code Playgroud)
谢谢
为什么NSDictionary
不能写?我已经检查了字典的内容:所有实例都是NSString
和的NSNumber
。我检查了权限:在相同路径下具有相同名称的文本文件写得很好。当然,我的字典不是空的。
NSString *file = ...
NSDictionary *dict = ...
// check dictionary keys
BOOL wrong = NO;
for (id num in [dict allKeys]) {
if (![num isKindOfClass:[NSNumber class]]) {
wrong = YES;
break;
}
}
if (wrong) {
NSLog(@"First");
}
// check dictionary values
wrong = NO;
for (id num in [dict allValues]) {
if (![num isKindOfClass:[NSString class]]) {
wrong = YES;
break;
}
}
if (wrong) {
NSLog(@"Second");
}
if (![dict writeToFile:file atomically:YES]) …
Run Code Online (Sandbox Code Playgroud) nsdictionary ×10
objective-c ×8
nsarray ×4
iphone ×3
ios ×2
ios5 ×2
cocoa-touch ×1
file ×1
macos ×1
nscopying ×1
null ×1
sorting ×1