iT邦幫忙

2

php 資料傳回手機之後要如何替代文字?

大家好,我今日在寫swift 時遇到的問題是,我將資料庫資料透過php抓回來之後,我想要將數字替代成文字,但是我不知道我要如何撰寫才可以成功替代掉,讓替代的文字顯示在模擬器上,先謝謝各位的回答!
ex: 傳回1則替換成文字今天 2則是明天 3則是後天 (1,2,3都是string,此欄位只會有1,2,3三個數字)
以下指想針對scene_no做上述的替換

import UIKit
    struct ScoreResults: Decodable {
        var User_na : String
        var Total : String
        var Scene_No : String
    }
class XXXTableViewController: UITableViewController {
    var username:[String]=[]
    var usertotal:[String]=[]
    var userscene:[String]=[]
    
    override func viewDidLoad() {
        super.viewDidLoad()
        getScoreResults()
    }

    func getScoreResults() {
        let address = "http://XXXXXX"
        if let url = URL(string: address) {
            URLSession.shared.dataTask(with: url) { (data, response, error) in
                if let error = error {
                    print("Error: \(error.localizedDescription)")
                } else if let response = response as? HTTPURLResponse,let data = data {
                    print("Status code: \(response.statusCode)")
                    let decoder = JSONDecoder()
                    
                    if let scoreResults = try? decoder.decode([ScoreResults].self, from: data) {
                        DispatchQueue.main.async{
                        for score in scoreResults {                  self.username.append(score.User_na)
                            self.usertotal.append(score.Total)
                            self.userscene.append(score.Scene_No)
                           
                        }
                            
                            self.tableView.reloadData()
                        }
                        
                    }
                }
            }.resume()
        } else {
            print("Invalid URL.")
        }
    }
    // MARK: - Table view data source

    override func numberOfSections(in tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return username.count
    }
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cellIdentifier = "Cell"
        let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as!getscoreTableViewCell
       
        cell.user.text = username[indexPath.row]
        cell.score.text = usertotal[indexPath.row]
        cell.scene.text = userscene[indexPath.row]
        return cell
    }

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中
1
海綿寶寶
iT邦大神 1 級 ‧ 2020-08-04 07:53:54
最佳解答

試試看

var username:[String]=[]
var usertotal:[String]=[]
var userscene:[String]=[]

加一列

var username:[String]=[]
var usertotal:[String]=[]
var userscene:[String]=[]
var arrtext = ["今天", "明天", "後天"]

然後

cell.scene.text = userscene[indexPath.row]

改成

cell.scene.text = arrtext[userscene[indexPath.row].toInt()-1];  //1,2,3 減1 成為 0,1,2
florarou iT邦新手 5 級 ‧ 2020-08-16 13:50:42 檢舉

你好!謝謝你的回覆 關於這個部分我目前解決了!謝謝你的幫忙!

florarou iT邦新手 5 級 ‧ 2020-08-16 13:53:12 檢舉

但是我想請問一下以下這種轉int方式跟您所回答的to.Int() 差別在哪裡嗎?

cell.scene.text = arrtext[Int(userscene[indexPath.row])!-1];

toInt()的寫法在 Swift 2.0 後就被取消了
現在的標準做法是你寫的 Int(String) 的寫法

1
var scoreResults: [ScoreResults]?

第一件事想問的就是為什麼你資料不這樣設定就好?
你後面的重組資料集合的用意???

然後codable來說,你接什麼資料,你struct就是要設對應的形態。這一點避免不了。
但你可以這樣做:

cell.score.text = "\(usertotal[indexPath.row])"

前提當然是你要確定這不是optional不然你還要處理nil的情形

最後其實應該這樣比較妥當:

let item = scoreResults[indexPath.row]
...
...

cell.user.text = item.User_na
cell.score.text = "\(item.Total)"
cell.scene.text = item.Scene_No

當然如果後端傳User_na我一定會罵他,名稱不能寫好寫滿嗎?
而且要用大寫就不要用底線,要用底線就不要用大寫。

cell.score.text = String(format: "%i",item.Total)

如果資料是整數的話也可以這樣用。

最後是我昨晚一直思考要怎樣回答你的依值轉字比較好(我比較喜歡人家多動點頭腦結果想太多我睡著了),想了想還是留這二行給你思考。

print(["零","壹","貳"][1])
print(["1":"今天","2":"明天","3":"後天"]["3"] ?? "")
florarou iT邦新手 5 級 ‧ 2020-08-16 13:44:11 檢舉

你好,很謝謝你給的建議,因為剛接觸程式語言我還有很多不足的地方需要學習,我會好好參考並做更改的! 我目前已解決了此項問題,謝謝你的幫忙!

2
japhenchen
iT邦超人 1 級 ‧ 2020-08-04 08:11:45

不要讓使用者有機會修改顯示或送出的資料,不管是root或越獄都不行,所以資料請在PHP端就轉好,包括所有下拉式選單、單選多選項、自動完成輸入的功能

想想都覺得可怕...

看更多先前的回應...收起先前的回應...

喔!傳回個1、2、3、4就可以嚇死你?

不是哦,我是說1代表什麼意思,2代表什麼意思,為什麼不能在伺服器端整理好才送回?要是有人做了類似的東西,錯誤的解釋了1、2、3的字面意思,結果造成畫面呈現跟可能的操作錯意,那不可怕嗎?

不可怕啊。
會做出錯的解釋就要怪自己不好好看技術文件吧。
有些東西,人家擺明就是不想直接讓你知道這些數字所代表的意義。
那不知道意義就是自己的問題了,那來的可怕?

還是其實只是自己想貪圖個方便而已?

而且什麼東西都讓伺服器來做。

想必是有一個(或很多個串聯)非常強大的伺服器在運作就是了。

我個人是不會把變數的解釋權交給客戶端,哪怕是手機app

哪天多了一個key:4,舊版的app裡都沒有辦法解釋時,就會出現undefined儲如此類,至於要如何更改字串,這就不是問題了,要用case..select,或replace string,各有各的玩法

可以不要帶火氣嘛?純討論,無關對錯,ok

沒有辦法解釋,那就是要你更新,很正常的事啊!

改個數字就很要人命的話應該每天都有工程師爆走了。

話說………

如果您覺得帶火氣~~~~~~我建議您去喝一杯涼的冷靜一下。

怎麼我沒上火的你上火了?

and 如果轉string什麼的不是問題……那您以為原作者是在問什麼問題呢?

如果只是依某值來更換字串,那也不是我想問的問題

我只是好奇,為什麼要把解釋關鍵值的事放在客戶端而已.

至於會不會暴走,我已經暴走好幾年了,接手維護別人的程式,最怕的就是
CASE WHEN 1 THEN 台北 ELSE 2 THEN 高雄
除了資料說明書,我真的無法猜到3是什麼,4又是什麼...

至於青草茶,一時買不到,冰水代替就好

可現在問問題的不是你,是原發問者。
如果你有問題,可以開一篇討論。

然後你所好奇的事…………這不是常態嗎?
我接過那麼多api和規格書,大都也有這種情形。
做為一個工程師根本不需要去懼怕這些。

東西怎麼來,我們就怎麼做。
缺東西的話那就是別人的責任,又不是我們的責任。

如果對方該告訴你1234是什麼,但是沒告訴你。

找得到就去譙對方。

找不到就放棄吧。

這個專案不做,還會有下一個。

1
YC
iT邦研究生 2 級 ‧ 2020-08-11 17:10:26
  1. 先用quicktype,建立ScoreResults,就會拿到
// This file was generated from JSON Schema using quicktype, do not modify it directly.
// To parse the JSON, add this file to your project and do:
//
//   let scoreResults = try? newJSONDecoder().decode(ScoreResults.self, from: jsonData)

import Foundation

// MARK: - ScoreResult
struct ScoreResult: Codable {
    let userNa, total, sceneNo: String

    enum CodingKeys: String, CodingKey {
        case userNa = "User_na"
        case total = "Total"
        case sceneNo = "Scene_No"
    }
}

typealias ScoreResults = [ScoreResult]

等一下,只要用
let scoreResults = try? JSONDecoder().decode(ScoreResults.self, from: jsonData)
就能解成ScoreResults的object
2. 依照你的需求,新增一個SceneNo,將1/2/3轉成今天/明天/後天

enum SceneNo: String, Codable {
    case 今天 = "1"
    case 明天 = "2"
    case 後天 = "3"
    
    var readableValue: String {
        switch self {
        case .今天:
            return "今天"
        case .明天:
            return "明天"
        case .後天:
            return "後天"
        }
    }
}

然後,將ScoreResult改成

struct ScoreResult: Codable {
    let userNa, total: String
    let sceneNo: SceneNo

    enum CodingKeys: String, CodingKey {
        case userNa = "User_na"
        case total = "Total"
        case sceneNo = "Scene_No"
    }
}
  1. 自定義一個可以自行處理ScoreResult的TableViewCell
class TableViewCell: UITableViewCell {
    @IBOutlet weak var user: UILabel!
    @IBOutlet weak var score: UILabel!
    @IBOutlet weak var scene: UILabel!
    
    var scoreResult: ScoreResult! {
        didSet {
            user.text = scoreResult.userNa
            score.text = scoreResult.total
            scene.text = scoreResult.sceneNo.readableValue
        }
    }
}
  1. 更新 table view,必須要在main thread,所以我習慣實作如下:
class TableViewController: UITableViewController {
    var scoreResults = ScoreResults() {
        didSet {
            DispatchQueue.main.async {
                self.tableView.reloadData()
            }
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        
        guard let url = URL(string: "https://gist.githubusercontent.com/yycking/634acc4074bf72bd28f05bb5a5472910/raw/33738085ce64936fa98ff97c2ccd391ad9684579/ScoreResults.json") else { return }
        URLSession.shared.dataTask(with: url) {[weak self] (data, _, error) in
            guard
                let data = data,
                let results = try? JSONDecoder().decode(ScoreResults.self, from: data) else {
                    return
            }
            self?.scoreResults = results
        }.resume()
    }
    
    // MARK: - Table view data source
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return scoreResults.count
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cellIdentifier = "Cell"
        let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as! TableViewCell
        cell.scoreResult = scoreResults[indexPath.row]
        
        return cell
    }
}
florarou iT邦新手 5 級 ‧ 2020-08-16 13:47:09 檢舉

謝謝你又提供了一個不一樣的做法,我會好好學習並作嘗試的!謝謝你!

我要發表回答

立即登入回答