void CountlyRecordEventSegmentationCountSum(const char * key, const char * segmentation, int count, double sum)
{
NSString * seg = CreateNSString(segmentation);
NSArray * entries = [seg componentsSeparatedByString:@"`"];
NSDictionary * dict = [NSDictionary dictionary];
for (id entry in entries)
{
NSArray * keyValue = [entry componentsSeparatedByString:@"|"];
[dict setValue:[keyValue objectAtIndex:1] forKey:[keyValue objectAtIndex:0]];
}
[[Countly sharedInstance] recordEvent:CreateNSString(key) segmentation:dict count:count sum:sum];
}
我把“?”因为我不完全确定问题是否在上面的代码中,但这是我最好的猜测。我正在将Countly iOS插件与Unity集成,其中一个Countly插件的方法以NSDictionary *
为参数。因为我不知道如何从C#发送字典到Objective-C我将我的dict存储在string
中,将其发送到Objective-C然后重新创建字典(上面的代码)。
但这可能甚至不相关。我知道EXC_BAD_ACCESS
通常与不同意的资源有关,或者也许你可以看到我做错了什么(我根本不知道Objective-C,只是编写了插件所需的几行)
编辑: 来自Unity示例:
// Converts C style string to NSString
NSString * CreateNSString (const char * string)
{
if (string)
return [NSString stringWithUTF8String: string];
else
return [NSString stringWithUTF8String: ""];
}
答案 0 :(得分:2)
您需要使用NSMutableDictionary
,而无法修改NSDictionary
。
此外,您应该使用setObject:forKey:
因为setValue:forKey:
是KVC方法。对于大多数密钥,它恰好在NSMutableDictionary
上执行相同的操作,但速度稍慢。
最后,在尝试访问索引0和1处的对象之前,应检查[keyValue count] >= 2
。
修改此外,CreateNSString()
看起来很可疑。它可能是泄漏或过早释放字符串。但是你需要发布代码。在任何情况下,我都会使用
seg = [NSString stringWithUTF8String: segment];
或其他适当的方法,如果段未以UTF-8编码。
答案 1 :(得分:2)
您所犯的错误是您尝试修改NSDictionary
的不可变版本。
初始化后,无法修改NSDictionary
的内容。您应该使用NSMutableDictionary
代替。
以下是NSMutableDictionary的文档。 这里是an example如何创建符合NSMutableCopying协议的不可变对象的可变版本。