我正在开发一个应用程序,它从API获取某些电影的信息,API返回每种类型的特殊int id中的电影类型(28个用于Action,12个用于Adventure)。我如何将这些ID更改为实际的流派名称?我尝试使用stringByReplacingOccurrencesOfString("\(GenreIDS)", withString: "\(GenreNames)")
(GenreIDS和GenreNames是数组),但我的应用程序崩溃了。
答案 0 :(得分:0)
没有一个调用(至少不是我所知道的)将一次性替换具有替换字符串数组的源字符串数组。方法stringByReplacingOccurrencesOfString
将一个字符串的所有实例替换为另一个字符串。
您需要遍历您的流派ID和流派名称数组并重复调用该函数。像这样:
for (index, genreID in genreIDs.enumerated() {
let genreName = genreNames[index]
let genreIDString = "\(genreID)"
let movieNames =
movieNames.stringByReplacingOccurrencesOfString(genreIDString,
withString: genreName)
}
(请注意,变量名和函数名应以小写字母开头,类名和类型名称应以大写字母开头。)