I have issues when I'm trying to create a custom object (UITextField) in Swift 3, it didn't happen before.
Asked
Active
Viewed 68 times
1 Answers
0
The code you provided is working fine the problem is somewhere else. You should use the default init with frame instead of the required one, or create your convenience init with desired properties.
class ThemeTextFieldCollab : UITextField {
override init(frame: CGRect) {
super.init(frame: frame)
self.attributedPlaceholder = NSAttributedString(string: self.placeholder ?? "", attributes: [NSForegroundColorAttributeName : UIColor.gray])
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
}

Gustavo Vollbrecht
- 3,188
- 2
- 19
- 37
-
I copy/pasted your code but I have an issue : "Value of type 'ThemeTextFieldCollab' has no member 'attributedPlaceholder'" At the line "self.attributedPlaceholder..." – Nuccle Jan 17 '19 at 13:51
-
UITextView does not have `attributedPlaceholder` or `placeholder`. – Gustavo Vollbrecht Jan 17 '19 at 13:54
-
ThemeTextFieldCollab is a TextField and not a TextView so I think your code is wrong – Nuccle Jan 17 '19 at 14:00
-
Ok, now I have the error: "Type 'NSAttributedString.Key' (aka 'NSString') has no member 'foregroundColor'" – Nuccle Jan 17 '19 at 14:06
-
That is because of your Swift version, replace `NSAttributedString.Key.foregroundColor` with `NSForegroundColorAttributeName`. I updated the answer code as well. – Gustavo Vollbrecht Jan 17 '19 at 14:09
-
I checked your source code again and it is working good as an `UITextField`, well, hope my answer was useful somehow since you shouldn't be using the required init. Check this: https://stackoverflow.com/questions/30085264/why-use-required-initializers-in-swift-classes – Gustavo Vollbrecht Jan 17 '19 at 14:23
-
Try with that super implementation. Both are working fine in my Xcode. – Gustavo Vollbrecht Jan 17 '19 at 14:34
-
I updated my post with a screenshot, It doesn't work, I don't know why :/ – Nuccle Jan 17 '19 at 14:38
-
if you just remove the required init, what does it show? Usually the quick fix provides a default required init implementation. – Gustavo Vollbrecht Jan 17 '19 at 14:42
-
I did what you said and I have exactly the same required init, with the error :/ – Nuccle Jan 17 '19 at 14:46
-
have you tried removing that SwiftTheme or any external library? – Gustavo Vollbrecht Jan 17 '19 at 14:48
-
or even cleaning the build folder. – Gustavo Vollbrecht Jan 17 '19 at 14:50
-
Keep in mind that your code is working fine. The problem is probably somewhere else. Try running this individually or fix the other problems in your code that you mentioned. Make sure everything that was opened is closed (, {, [, ... – Gustavo Vollbrecht Jan 17 '19 at 14:58
-
Yes it works because I test my code source on the friend's mac and it works. I don't know why there is error when it's on my mac. – Nuccle Jan 17 '19 at 15:59