-5

I cannot seem to get rid of the "optional" label in the following label. I've looked at different resources but couldn't figure out what to implement. Could you please show me where I'm mistaken?

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
    Look up "Optionals" in the Swift book by Apple and you will get the answer (and a lot more). – dasdom Oct 07 '17 at 09:21
  • set "!" mark at end of the value make sure value will not be **nil** other wise you can use "?? "Value not found"" – iPatel Oct 07 '17 at 09:25
  • and name you variables, in your case `ServicePoint` starting with a lower case `servicePoint`. Not a must, but will make your life easier. – Au Ris Oct 07 '17 at 09:36
  • I cannot reproduce your issue. What does `print(self.ratingTexts[Int(rateValue)])` show? – Martin R Oct 07 '17 at 09:42

1 Answers1

0

To unwrap do as follow

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

Example

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)

Output:

enter image description here

Note:

If you have array of dict , array of array, dict of dict, dict of array, then you have to unwrap like below:

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

Doesn't matter how much array or dict are complex, always follow above.

If you have still doubt then you can ask.

dahiya_boy
  • 9,298
  • 1
  • 30
  • 51
  • `String(format:, ...)` does *not* return an optional. – Martin R Oct 07 '17 at 09:59
  • @MartinR Hope Now its OK. But whenever I used `String(format:,..)`, xcode used this to avoid warning and it always returns optional. Can you pls explain me where i am wrong? – dahiya_boy Oct 07 '17 at 10:21
  • https://developer.apple.com/documentation/swift/string/1417691-init does not return an optional, and actually I cannot reproduce OP's problem with the provided code. So either I am overlooking something, or OP did not show the real code. – Martin R Oct 07 '17 at 10:24