Home > other >  set Label a String which is in CoreData
set Label a String which is in CoreData

Time:12-02

I'm trying to set UILabel.text a string which is located into coredata, but I get an error which says:Instance member "test1" cannot be used on type 'ProfileViewController' did you mean to use a value of this type instead? any solutions?

    override func viewWillAppear(_ animated: Bool) {
        let request = NSFetchRequest<NSFetchRequestResult>(entityName: "Users")
                request.returnsObjectsAsFaults = false
                do {
                    let result = try context.fetch(request)
                    for data in result as! [NSManagedObject] {
                        test1 = data.value(forKey: "username") as! String
                  }
                } catch {
                    print("Failed")
                }
    }
    var test1 = String()
    let nameLabel: UILabel = {
        let label = UILabel()
        label.textAlignment = .center
        label.text = test1  //error is here
        label.font = UIFont.boldSystemFont(ofSize: 26)
        label.textColor = .white
        return label
    }()

CodePudding user response:

You don't need to refer to test1 when you create the label. The text property of a UILabel is an optional string, so not assigning a value is perfectly legal.

Even if you could/did assign test1, it won't help you achieve what you want since UIKit doesn't work with bindings; assigning a new value to test1 in viewDidAppear won't change the value in the label.

You also need to add your nameLabel to the view controllers view and either set some constraints or set its frame; I will assume you have done this in viewDidLoad but not shown that here.

All you need is:

let nameLabel: UILabel = {
    let label = UILabel()
    label.textAlignment = .center
    label.font = UIFont.boldSystemFont(ofSize: 26)
    label.textColor = .white
    return label
}()

override func viewWillAppear(_ animated: Bool) {
    let request = NSFetchRequest<NSFetchRequestResult>(entityName: "Users")
    request.returnsObjectsAsFaults = false
    do {
        let result = try context.fetch(request) 
        if let result = result as? [NSManagedObject] {
        for data in result {
            self.nameLabel.text = data.value(forKey:"username") as? String
        }
    } catch {
        print("Failed")
    }

    super.viewDidAppear(animated) // Don't forget to call super
}

If you did want to keep the name in a property, then you could implement a pseudo-binding with a didSet function:


let nameLabel: UILabel = {
    let label = UILabel()
    label.textAlignment = .center
    label.font = UIFont.boldSystemFont(ofSize: 26)
    label.textColor = .white
    return label
}()

var name: String? {
    didSet {
        self.nameLabel.text = name
    }
}

override func viewWillAppear(_ animated: Bool) {
    let request = NSFetchRequest<NSFetchRequestResult>(entityName: "Users")
    request.returnsObjectsAsFaults = false
    do {
        let result = try context.fetch(request) 
        if let result = result as? [NSManagedObject] {
        for data in result {
            self.name = data.value(forKey:"username") as? String
        }
    } catch {
        print("Failed")
    }

    super.viewDidAppear(animated) // Don't forget to call super
}

CodePudding user response:

Try this:

override func viewDidAppear() {
    super.viewDidLoad()
    test()                                 // run the func here 
}


func test() {
    var test1 = String()
    let nameLabel: UILabel = {
        let label = UILabel()
        label.textAlignment = .center
        label.text = test1  //error is here
        label.font = UIFont.boldSystemFont(ofSize: 26)
        label.textColor = .white
        return label
    }()
}
  • Related