Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift cannot assign value of type '()' to type 'String?'

Tags:

ios

uilabel

swift

I'm learning Swift2/iOS app development. I am confused by an error being thrown by Xcode before compiling. Here is the code throwing the error :

let dotpos = display.text!.rangeOfString(".")
if dotpos != nil {
    display.text = display.text!.removeRange(dotpos!)
}

The error thrown is (at the line "display.text = display.text!.removeRange(dotpos!)") :

Cannot assign value of type '()' to type 'String?'

Note : display is a UILabel object.

Could someone point me toward the error I might have done?

like image 469
Cedric Avatar asked May 13 '16 15:05

Cedric


1 Answers

you need to check documentation for this (Apple swift String link)

let dotpos = display.text!.rangeOfString(".")
if dotpos != nil {
    display.text!.removeRange(dotpos!)
}

This code will work, removeRange function didn't return anything, documentation said

mutating func removeRange(_ subRange: Range)

means text mutate when you call the method on your text label.

The text change directly and you don't need to assign new value for changing it.

like image 93
guiltance Avatar answered Oct 07 '22 14:10

guiltance