gpt4 book ai didi

swift - 您将在 Swift for XML 中的何处包含替换事件?

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

我正在尝试显示一个简单的数组。我已经解析了所有内容,但是我想使用 replacingOccurrences 将成分显示在另一个下面,问题是我不确定将其放入代码中的位置。

一些效果:

let newString = RecipeIngredients.replacingOccurrences(of: ", ", with: "\n")

我只是不确定在哪里包含此行

我认为它位于 XML 解析器代码中的某个位置,但我没有运气。

食谱.xml

<dish>
<title> Pancakes </title>
<calories> 350 calories </calories>
<ingredients> ing 1, ing 2, ing 3 </ingredients>
</dish>

XMLParse.swift

struct Recipes {
var title = ""
var calories = ""
var ingredients = ""
}

RecipeTableViewController.swift

class RecipeTableViewController: UITableViewController, XMLParserDelegate {

@IBOutlet var myTableView: UITableView!

var tableViewDataSource = [Recipes]()

var thisName = ""
var recipeTitle = ""
var recipeCalories = ""
var recipeIngredients = ""

override func viewDidLoad() {
super.viewDidLoad()

myTableView.dataSource = self
myTableView.delegate = self

if let path = Bundle.main.url(forResource: "Recipes", withExtension: "xml") {
if let parser = XMLParser(contentsOf: path) {
parser.delegate = self
parser.parse()
}
}
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()

}

// Table View Delegates

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

return tableViewDataSource.count
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)

let titleLabel = cell.viewWithTag(11) as! UILabel
let caloriesLabel = cell.viewWithTag(12) as! UILabel

titleLabel.text = tableViewDataSource[indexPath.row].title
caloriesLabel.text = tableViewDataSource[indexPath.row].calories

return cell
}

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let Storyboard = UIStoryboard(name: "Main", bundle: nil)
let resultsVC = Storyboard.instantiateViewController(withIdentifier: "ResultsViewController") as! ResultsViewController

// Information to be passed to ResultsViewController
resultsVC.getTitle = tableViewDataSource[indexPath.row].title
resultsVC.getIngredients = tableViewDataSource[indexPath.row].ingredients

// Push to next view
self.navigationController?.pushViewController(resultsVC, animated: true)

}

override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 100
}

// XML Parser

func parser(_ parser: XMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [String : String] = [:]) {
thisName = elementName

if elementName == "dish" {
var recipeTitle = ""
var recipeCalories = ""
var recipeIngredients = ""
}
}

func parser(_ parser: XMLParser, foundCharacters string: String) {
let data = string.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines)

if data.count != 0 {
switch thisName
{
case "title": recipeTitle = data
case "calories": recipeCalories = data
case "ingredients": recipeIngredients = data
default:
break
}
}
}

func parser(_ parser: XMLParser, didEndElement elementName: String, namespaceURI: String?, qualifiedName qName: String?) {
if elementName == "dish" {
var recipe = Recipes()
recipe.title = recipeTitle
recipe.calories = recipeCalories
recipe.ingredients = recipeIngredients

print(recipe)
tableViewDataSource.append(recipe)
}
}
}

print(recipe) 返回:食谱(标题:“薄煎饼”,卡路里:“350卡路里”,成分:“ing 1,ing 2,ing 3”)

我知道这可以作为数组来完成,但我也想弄清楚这种格式。谢谢!

最佳答案

为需要的人解决了我自己的问题

case "ingredients":
recipeIngredients = data
recipeIngredients = recipeIngredients.replacingOccurrences(of: ", ", with: "\n")

关于swift - 您将在 Swift for XML 中的何处包含替换事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49844763/

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