gpt4 book ai didi

swift - 如何用相应的图像名称替换结构中的字符串名称?

转载 作者:行者123 更新时间:2023-11-30 12:48:42 25 4
gpt4 key购买 nike

我有一个结构来存储已捆绑在应用程序中的图片的名称和图像名称,如下所示

struct City { var name: String
var imageName: String }

class firstViewController: UIViewController

let cities = [City(name:"City00", imageName:"city_00"),
City(name:"City01", imageName:"city_01"),
City(name:"City02", imageName:"city_02")]

在displayViewController中,我有一个变量nowCityImages来存储从firstViewcontroller选择的数据

class displayViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, NSFetchedResultsControllerDelegate {

var nowCityImages: [String] = []

现在我想在桌面 View 中显示城市图像

public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let myCell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! CityCell

let outCityImages = nowCityImages[indexPath.item]

// This is not displaying any images
myCell.cityImage.image = UIImage(named:outCityImages)

return myCell
}

如果我使用 print(outCityImages),我将得到以下异常(exception)输出

City00
City02

但我不知道如何在表格 View 单元格中将 City00 替换为 city_00 并将 City02 替换为 city_02 以便显示图像。

最佳答案

如果末尾的数字长度为 2 位:

var str = "City00"
let index = str.index(str.endIndex, offsetBy: -2)

var str1 = str.substring(to: index).lowercased()
var str2 = str.substring(from: index)
let name = "\(str1)_\(str2)" //city_00

关于swift - 如何用相应的图像名称替换结构中的字符串名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41297576/

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