gpt4 book ai didi

ios - 为什么我的代码只打印 Xcode 中数组中的第一个元素?

转载 作者:行者123 更新时间:2023-11-30 17:44:30 24 4
gpt4 key购买 nike

生成的代码仅打印出 Xcode 中数组的第一个元素,即使我向它传递了三个单词。有什么原因吗?

char str[100] = {0};

NSLog(@"Please enter three words: ");
scanf("%s", str);
NSString *words = [NSString stringWithUTF8String: str];

NSMutableArray *mutableWords = [[words componentsSeparatedByString: @" "] mutableCopy];

NSLog(@"%@", mutableWords);

最佳答案

fgets(str, sizeof(str), stdin);    

这会将整行读入 str

<小时/>

摆脱作为数组最后一个索引的 '\n' 字符的最简单方法是简单地更改拆分数组的方式:

NSMutableArray *mutableWords = [[words componentsSeparatedByCharactersInSet:
[NSCharacterSet whitespaceAndNewlineCharacterSet]] mutableCopy];

关于ios - 为什么我的代码只打印 Xcode 中数组中的第一个元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19968745/

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