gpt4 book ai didi

ios - 字符串到字典和向后

转载 作者:行者123 更新时间:2023-11-29 10:43:08 25 4
gpt4 key购买 nike

我为 ESTIMOTE Beacons 设计了一个解决方案,它不会在你每次经过该区域时打扰你,但我是 Objective C、iOS、iPhone 等的新手,我无法在 Objective-C 中真正工作。我们在这里假设我们在区域中只有一个信标。因此,每次您进入一个区域时,它都会开始测距,找到唯一的 1 个信标并执行以下操作:检查特定文件是否在特定路径上,如果不在特定路径上,则创建文件并将字符串放入其中,如“major_as_int:actual_time;”,通过 get 变量将主要值发送到 .php 文件,然后 php 完成工作。

这是我的第一个问题。如何获取格式为“23:10:01”的时间?

如果文件已经存在,检查它是否为空,如果文件不空,我的第二个问题来了....

如何将字符串转换为字典、编辑字典,然后将字典转换回字符串,以便用从字典中提取的新字符串替换文件中的旧字符串?

例如:我的文件中已经有一个字符串“12345:12:45:12;54321:20:15:01;90900:05:21:13;”我想用它制作字典 { 12345 : 12:45:12, 54321 : 20:15:01, 90900 : 05:21:13 }。主值应该是字典中的整数,所以我可以问是字典中的实际主值作为键。如果是,请检查它存在了多长时间,如果超过 24 小时或更长时间,请再次执行 php。如果没有,什么都不做。将相同的字符串返回到文件。如果实际主值不在字典中,则将实际主值添加为具有实际时间值的新键。字典看起来像 { 12345 : 12:45:12, 54321 : 20:15:01, 90900 : 05:21:13, 88771 : 10:12:33 }。然后字典被转换回字符串“12345:12:45:12;54321:20:15:01;90900:05:21:13;88771:10:12:33;”

一些代码片段很高兴看到,但我愿意学习,因为这是我 future 的工作,所以请告诉我正确的方向,不要发布太多代码。或者我可能以错误的方式攻击问题?提前致谢。

最佳答案

这应该可以将字符串转换成字典(虽然还没有测试过)。从这个例子中应该清楚反向转换。

    NSString *string1 = @"12345:12:45:12;54321:20:15:01;90900:05:21:13;";
NSArray *components1 = [string1 componentsSeparatedByString:@";"];

NSMutableDictionary *mutableDictionary = [NSMutableDictionary dictionary];
for (NSString *string2 in components1) {
NSArray *components2 = [string2 componentsSeparatedByString:@":"];
NSMutableArray *mutableComponents2 = [components2 mutableCopy];
NSString *majorValue = [mutableComponents2 firstObject];

// Check whether there is actually a majorValue
if (majorValue) {
[mutableComponents2 removeObjectAtIndex:0];
mutableDictionary[majorValue] = [mutableComponents2 componentsJoinedByString:@":"];
}
}

NSLog(@"mutableDictionary: %@", mutableDictionary);

编辑:如果您的字符串看起来像 123#12:12:12; 这将更改为

    NSString *string1 = @"12345#12:45:12;54321#20:15:01;90900#05:21:13;";
NSArray *components1 = [string1 componentsSeparatedByString:@";"];

NSMutableDictionary *mutableDictionary = [NSMutableDictionary dictionary];
for (NSString *string2 in components1) {
NSArray *components2 = [string2 componentsSeparatedByString:@"#"];
NSMutableArray *mutableComponents2 = [components2 mutableCopy];
NSString *majorValue = [mutableComponents2 firstObject];

// Check whether there is actually a majorValue
if (majorValue) {
mutableDictionary[majorValue] = [mutableComponents2 lastObject];
}
}

NSLog(@"mutableDictionary: %@", mutableDictionary);

关于ios - 字符串到字典和向后,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23489579/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com