Pg78 error with rangeOfString

#1

I’m fairly new to coding which could be the issue here, but to me it looks like a problem of trying to turn an older version of swift to work with 3.

I got the printing to console to work from the previous page with the print lines, but when I replace them is where I run into trouble. Currently with as follows:

func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool{

    let existingTextHasDecimalSeparator = textField.text?.rangeOfString(".")
    let replacementTextHasDecimalSeparator = string.rangeOfString(".")
    
    if existingTextHasDecimalSeparator != nil && replacementTextHasDecimalSeparator != nil{
        return false
    }
    else{
        return true
    }
    }

On both lines of code beginning with let, I am getting an error – Value of type ‘String’ has no member 'rangeOfString’
I tried with string.range(of: String) but it leads to another error – Cannot call value of non-function type ‘Range<String.Index>?’

I’m also getting a yellow instance method error for the func line - it keeps wanting me to change it.

I don’t currently have written in code: textField(_:shouldChangeCharactersInRange: replacementString:) because when reading I didn’t think it meant for me to actually type this in anywhere.

I have been able to work through a lot of the other errors from swift issues, but got stuck here. Any help? Thanks.

#2

You probably aren’t importing the library that provides the method:

import Foundation

let myString = "hello"
let x = myString.range(of: "ll")

if (x != nil) {
    print("It worked!")
}

--output:--
It worked!

That code works fine at http://swiftlang.ng.bluemix.net. But if I eliminate the import, I get the error:

value of type ‘String’ has no member 'range’
let x = myString.range(of: “ll”)

#3

Thanks for your response. That got rid of the no member error, but now I have a new error---- Cannot convert value of type ‘String.Type’ to expected argument type ‘String’

Unfortunately I have managed to somehow delete my entire storyboard. It is still letting my adjust the code and check for errors. None of the connections to the view controller seem to be giving me any errors though which is nice.

#4

It’s hard to debug imaginary code. Nevertheless, I believe your error has to do with spelling. Spelling counts in computer programming, e.g. writing String instead of string is not the same thing.

For your information, iOS programming is for advanced intermediates/advanced programmers, e.g. you have at least three years of programming in other languages including some GUI programming–even then you will struggle to learn iOS programming. Beginners have no chance.

#5

Hey there,

Thanks for posting and struggling through. After a ton of searching, I found how to fix this error that plagued mine as well. I’ll post my entire code in there, and you can let me know if it works for you. I’m having an actual error when I run the code and click in the textbox, but I’m hoping I’ll find the solution for that later.

Basically, rangeOfString has now become range(of:string). But the “string” referenced in (of:string) is actual text. Thus, it becomes range(of:".").

import UIKit

class conversionViewController: UIViewController, UITextFieldDelegate {

@IBOutlet var celsiusLabel: UILabel!
@IBOutlet var textField: UITextField!

//From the message board

// @objc protocol UITextFieldDelegate {
// @objc optional func textFieldShouldBeginEditing(textField: UITextField) -> Bool
// @objc optional func textFieldDidBeginEditing(textField: UITextField)
// @objc optional func textFieldShouldEndEditing(textField: UITextField) -> Bool
// @objc optional func textFieldDidEndEditing(textField: UITextField)
// @objc optional func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange,
// replacementString string: String) -> Bool
// @objc optional func textFieldShouldClear(textField: UITextField) -> Bool
// @objc optional func textFieldShouldReturn(textField: UITextField) -> Bool
// }
//End From Mesasge Board

func textField(_ textField: UITextField,
               shouldChangeCharactersIn range: NSRange,
               replacementString string: String) -> Bool {

    let existingTextHasDecimalSeparator = textField.text?.range(of:".")
    let replacementTextHasDecimalSeparator = string.range(of:".")
    
    if existingTextHasDecimalSeparator != nil &&
        replacementTextHasDecimalSeparator != nil {
        return false
    }
    else {
        return true
    }

}

let numberFormatter: NumberFormatter = {
    let nf = NumberFormatter()
    nf.numberStyle = .decimal
    nf.minimumFractionDigits = 0
    nf.maximumFractionDigits = 1
    return nf
} ()

var fahrenheitValue: Double? {
    didSet {
        updateCelsiusLabel()
    }
}

var celsiusValue: Double? {
    if let value = fahrenheitValue {
        return (value - 32) * (5/9)
    }
    else {
        return nil
    }
}

func updateCelsiusLabel() {
    if let value = celsiusValue {
        // Edited from website
        celsiusLabel.text = numberFormatter.string(from: NSNumber(value: value))
    }
    else {
        celsiusLabel.text = "???"
    }
}

@IBAction func dismissKeyboard(sender: AnyObject) {
    textField.resignFirstResponder()
}

@IBAction func fahrenheitFieldEditingChanged(textField: UITextField) {
    
    if let text = textField.text, let value = Double(text) {
        fahrenheitValue = value
    }
    else {
        fahrenheitValue =  nil
    }
}

}

#6

I see you post a lot on here, and thank you for your help.

Funny, the authors of the book explicitly state that after you complete the first book on Objective-C programming that you will be ready to attempt to handle their iOS programming. In fact, throughout each book the mantra “anyone can learn programming” appears throughout. They recognize it will be a struggle and even talk about lifelong learning. Should I write the authors and demand a refund?

For your information, most of the noobs on this board aren’t trying to become the next Steve Jobs. We realize that this is just scratching the surface. Posts that don’t answer questions or nebulously say things like “you should go play around on Swift for three years” don’t add value. It reminds me of some of the lousy professors I had in college and law school who couldn’t answer basic questions, and were insulting and dismissive. I’m glad those days are behind me, and enjoy this forum as a way of trying to learn a new skill. I’ll kindly ask that you keep that in mind when the novices of the board ask simple questions.

#7

Thanks for the help Hammer, it was much appreciated!