Instance member 'layer' cannot be used on type 'CustomPrettyView'

Viewed 242
import UIKit 
class CustomPrettyView: UIView { 
    override class func awakeFromNib() {
        layer.cornerRadius = 20 
    } 
}

Why it does not work?

1 Answers

class func declare type level methods. They are similar to static func but class func methods can be overridden by subclasses.

There are two implementations of func awakeFromNib that you can override:

  • one of them is instance level method
  • and the other is type level method.

Only instance level method has access to non-static members of a class like layer, mask, supreview and so on. Read here more about type level methods: https://docs.swift.org/swift-book/LanguageGuide/Methods.html

Thus, by removing class keyword you'll fix the issue:

    override func awakeFromNib() {
       super.awakeFromNib()
       // rest of code
     }
Related