Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 为我的数组字典创建唯一键_Ios - Fatal编程技术网

Ios 为我的数组字典创建唯一键

Ios 为我的数组字典创建唯一键,ios,Ios,有人能告诉我如何创建当前日期和时间作为字典的唯一键吗。我正在使用下面的代码,但当间隔几秒钟输入时,键不是唯一的 NSDateFormatter *currDate =[[NSDateFormatter alloc]init]; [currDate setDateStyle:NSDateFormatterFullStyle]; [self.transDict setObject:self.transArray forKey:[NSString stringWithFormat:@"%@",

有人能告诉我如何创建当前日期和时间作为字典的唯一键吗。我正在使用下面的代码,但当间隔几秒钟输入时,键不是唯一的

NSDateFormatter *currDate =[[NSDateFormatter alloc]init];

[currDate setDateStyle:NSDateFormatterFullStyle];

[self.transDict setObject:self.transArray forKey:[NSString stringWithFormat:@"%@",
    [currDate stringFromDate:[NSDate date]]]];
正如你所知道的,我是一个新手…感谢你的帮助
thx

首先,您应该了解格式化程序是否正常工作

将最后一行更改为如下所示:

NSString * currentDateAsString = [currDate stringFromDate:[NSDate date]];
if(currentDateAsString)
{
    [self.transDict setObject: self.transArray forKey: currentDateAsString];
} else {
    NSLog( @"why did my date formatter fail?" );
}
您不需要执行
[NSString stringWithFormat:…
位,因为
stringFromDate
已返回一个
NSString
对象,这就是密钥所需的全部内容


最后,如果您没有使用ARC,请不要忘记发布您创建的日期格式化程序(通过alloc&init)。

首先,您应该了解格式化程序是否正常工作

将最后一行更改为如下所示:

NSString * currentDateAsString = [currDate stringFromDate:[NSDate date]];
if(currentDateAsString)
{
    [self.transDict setObject: self.transArray forKey: currentDateAsString];
} else {
    NSLog( @"why did my date formatter fail?" );
}
您不需要执行
[NSString stringWithFormat:…
位,因为
stringFromDate
已返回一个
NSString
对象,这就是密钥所需的全部内容


最后,如果您没有使用ARC,请不要忘记发布您创建的日期格式化程序(通过alloc&init).

谢谢您的评论。我输入了一些NSLog输出,表明我的密钥正在更改,但dict的计数保持不变。看起来我还有更多的研究要做。日期似乎不是用作密钥的好字符串。谢谢您的评论。我输入了一些NSLog输出,表明我的密钥正在更改,但数字e dict保持不变。看起来我还有更多的研究要做。日期似乎不是一个好的字符串,不能用作键。