以String形式访问自定义对象属性(NSMutableArray)

时间:2016-12-07 00:41:04

标签: ios swift

我有一个公司类,每个公司都有一个名称,徽标和股​​票价格,所有类型都是String。它们保存在NSMutableArray中。

我试图在tableview中访问这些属性,并在标签等中使用它们。

这是我公司的课程:

//code I am trying to replace

public String delete(@RequestParam(value="orderId", required=false, defaultValue="-1") int orderId, Model model) {
SomeApp.getStore().getOrderList().removeIf(p -> (p.getId() == orderId));
model.addAttribute("orderList", SomeApp.getStore().getOrderList());

//with this
public String orderDelete(Map<String, Object> model) {<--does this always stay this way?
    int orderID= OrderService.delete(orderId);
    orderRepository.delete(orderID)
    return "someform/orderMaster";
} 

在我的CompanyController中,我初始化

import UIKit
import Alamofire
import SwiftyJSON



var companyController: CompanyController?

var companies:NSMutableArray? = NSMutableArray()

var applePrice: String?
var googlePrice: String?
var twitterPrice: String?
var teslaPrice: String?
var samsungPrice: String?

var stockPrices = [String]()

let stockUrl = "https://query.yahooapis.com/v1/public/yql?q=select%20symbol%2C%20Ask%2C%20YearHigh%2C%20YearLow%20from%20yahoo.finance.quotes%20where%20symbol%20in%20(%22AAPL%22%2C%22GOOG%22%2C%22TWTR%22%2C%22TSLA%22%2C%20%22SSNLF%22)&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys"



class Company: NSObject {

var companyName: String?
var companyLogo: String?
var stockPrice: String?
init(companyName:String, companyLogo:String, stockPrice:String) {
    self.companyName = companyName
    self.companyLogo = companyLogo
    self.stockPrice = stockPrice

    companies?.add(apple)
    companies?.add(google)
    companies?.add(twitter)
    companies?.add(tesla)
    companies?.add(samsung)
}
}




func stockFetcher() {

Alamofire.request(stockUrl).responseJSON { (responseData) -> Void in
    if((responseData.result.value) != nil) {
        let json = JSON(responseData.result.value!)
        if let appleStockPrice = json["query"]["results"]["quote"][0]["Ask"].string {
            print(appleStockPrice)
            applePrice = appleStockPrice
        }
        if let googleStockPrice = json["query"]["results"]["quote"][1]["Ask"].string {
            print(googlePrice)
            googlePrice = googleStockPrice
        }
        if let twitterStockPrice = json["query"]["results"]["quote"][2]["Ask"].string {
            print(twitterStockPrice)
            twitterPrice = twitterStockPrice
        }
        if let teslaStockPrice = json["query"]["results"]["quote"][3]["Ask"].string {
            print(teslaStockPrice)
            teslaPrice = teslaStockPrice
        }
        if let samsungStockPrice = json["query"]["results"]["quote"][4]["Ask"].string {
            print(samsungStockPrice)
            samsungPrice = samsungStockPrice
        }
        let stockPriceArray = [applePrice, googlePrice, twitterPrice, teslaPrice, samsungPrice]
        stockPrices = stockPriceArray as! [String]
        companyController?.tableView.reloadData()
        print(json)
    }
}
}


let apple = Company(companyName: "Apple", companyLogo: "AppleLogo", stockPrice: applePrice!)
let google = Company(companyName: "Google", companyLogo: "GoogleLogo", stockPrice: googlePrice!)
let twitter = Company(companyName: "Twitter", companyLogo: "TwitterLogo", stockPrice: twitterPrice!)
let tesla = Company(companyName: "Tesla", companyLogo: "TeslaLogo", stockPrice: teslaPrice!)
let samsung = Company(companyName: "Samsung", companyLogo: "SamsungLogo", stockPrice: samsungPrice!)

现在想在单元格中使用公司信息,例如在cellForRowAt indexPath中我想将公司名称分配给文本标签。

var company = [Company]()

如何访问NSMutableArray中的公司属性以获取名称等?我希望能够做一些像

这样的事情
    let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as! CompanyCell

    //Text label
    cell.textLabel?.text = <what goes here?>[indexPath.row]

1 个答案:

答案 0 :(得分:0)

如果您需要“company.CompanyProperty”,您可以创建一个阵列并放置所有公司。 在该功能中,您可以轻松获得所有礼仪:

cell.textLabel?.text = [indexPath.row].companyName
cell.textLabel?.text2 = [indexPath.row].companyLogo
cell.textLabel?.text3 = [indexPath.row].stockPrice