可选字符串 - 视图控制器

时间:2017-10-07 09:20:12

标签: ios swift

我似乎无法摆脱"可选"标签在以下标签中。我查看了不同的资源,但无法确定要实施的内容。你能告诉我我错在哪里吗?

Cannot get rid of "Optional" label.

class RatingViewController: UIViewController {



@IBOutlet weak var rateLabel: UILabel!
@IBOutlet weak var rateView: EmojiRateView!


var ServicePoint: String!

 var ref:DatabaseReference?


let ratingTexts = [NSLocalizedString("Çok Kötü", value:"Very Bad", comment: ""), NSLocalizedString("Kötü", value:"Bad", comment:""), NSLocalizedString("Normal", value:"Normal", comment:""), NSLocalizedString("İyi", value:"Good", comment:""), NSLocalizedString("Çok iyi", value:"Very good", comment:""), NSLocalizedString("Mükemmel", value:"Perfect", comment:"")]


var newRateSyting = String("%.2f, %@")!



override func viewDidLoad() {
    super.viewDidLoad()


    rateView.rateValueChangeCallback = {(rateValue: Float!) -> Void in

        self.rateLabel?.text = String(format: self.newRateSyting, rateValue, self.ratingTexts[Int(rateValue)])

}

}

1 个答案:

答案 0 :(得分:0)

要打开,请执行以下操作

let strRating =  "\(self.newRateSyting!)\(rateValue!)\(self.ratingTexts[Int(rateValue)]!)"
self.rateLabel?.text = strRating

示例

let value : String? = "4.7"
let review : String? = "very good"

let strRate = "\(value), \(review)"

print("Before unwrap")

print(strRate)

print("\n After unwrap")
let strRate2 = "\(value!), \(review!)"
print(strRate2)

<强>输出:

enter image description here

注意:

如果你有 dict数组数组数组 dict的dict dict of array ,然后你必须打开如下:

(arr[index]["key"])! , (arr[index][index])!, (arr["key"]["key"])!, (arr["key"][index])!

无论阵列或词典有多复杂,无论如何都要遵循以上原则。

如果你还有疑问,那么你可以问。