gpt4 book ai didi

ios - 如何将 int 转换为 id?

转载 作者:行者123 更新时间:2023-11-28 18:22:20 25 4
gpt4 key购买 nike

我有一个数组,它的内容是id类型的对象,但我需要把它们变成int类型。有什么方法可以让数组将数据读取为 int,或者将 id 转换为 int

    NSArray *array = [string componentsSeparatedByString:@"\n"];

int foo = array[0]; /*Warning: Incompatible pointer to integer conversion initializing 'int' with an expression of type 'id' */

最佳答案

componentsSeparatedByString: 文档说:

Return value

An NSArray object containing substrings from the receiver that have been divided by separator.

因此您的 fileContents 包含一个 NSStrings 数组。 fileContents[0] 是数组中的第一个 NSString 实例。您可以通过调用将 NSString 转换为 int 或最好是 NSInteger

[string intValue];
[string integerValue];

所以你的代码应该是这样的(假设数组至少包含 1 个对象,不要忘记检查这个):

int object1 = [fileContents[0] intValue];

或者甚至更好地包括类型转换以获得更好的代码可读性

int object1 = [(NSString *)fileContents[0] intValue];

关于ios - 如何将 int 转换为 id?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18017456/

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