Error "Value of type ... has no member" while calling self in lazy property closure - swift3

Why the following Swift3 code results in error value of type (NSObject) -> () -> MyView has no member getSomeButton in Xcode 8?
lazy var someButton = {
return self.getSomeButton // ERROR
}()
private func getSomeButton() -> UIButton {
return UIButton()
}

Actually, the error message is totally misleading and should be something like can't infer closure return type. Specifying variable type solves the problem. Hope this help someone.
lazy var someButton: UIButton = {
return self.getSomeButton // OK
}()

Related

Cannot convert call result type 'Set<String>' to expected type 'String' error

I am working on an old Swift 3 project and I'm getting this error after updating it to Swift 4.2. It seems to work fine in Swift 3. I had to declare let NSURLPboardType = NSPasteboard.PasteboardType(kUTTypeURL as String) because NSURLPboardType does not exist in Swift 4.2 but otherwise the code is the same.
enum SparkleDrag {
static let type = "com.razeware.StickerDrag.AppAction"
static let action = "make sparkles"
}
let NSURLPboardType = NSPasteboard.PasteboardType(kUTTypeURL as String)
var nonURLTYpes: Set<String> {return [String(kUTTypeTIFF), SparkleDrag.type]}
var acceptableTypes: Set<String> {return [nonURLTYpes.union(NSURLPboardType)]}
The "u" in union is underlined with the error but I don't quite understand the nature of the problem. Any guidance would be much appreciated.
The problem is that NSURLPboardType is not a Set<String>, so the union cannot work.
If you're trying to get something like this:
["com.razeware.StickerDrag.AppAction", "public.url", "public.tiff"]
in aceptableTypes, you can simply forgo NSURLPboardType and do this:
enum SparkleDrag {
static let type = "com.razeware.StickerDrag.AppAction"
static let action = "make sparkles"
}
// let NSURLPboardType = NSPasteboard.PasteboardType(kUTTypeURL as String)
var nonURLTYpes: Set<String> {return [String(kUTTypeTIFF), SparkleDrag.type]}
var acceptableTypes: Set<String> {return nonURLTYpes.union([kUTTypeURL as String])}

Swift. Error in working with UIApplication.shared

This question, in the old program that is implemented under UIKit, was done like this.
Condition in which the following actions are performed:
if(theApp().m_Disp.Connecttosrv(self.SelCon)) {
In the condition, the function is accessed
func theApp() -> iSPultApp!
{
return UIApplication.shared as? iSPultApp
}
Where next the class is called from the function
class iSPultApp: UIApplication {
var m_Disp: Chroot = Chroot()
Everything works there, is it possible to redo it somehow for SwiftUI?
The program goes to func theApp (), and then instead of going to the class, returns to the condition and there is an error:
if(theApp().m_Disp.Connecttosrv(self.SelCon)) {
Thread 1: Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value.
self.SelCon is not empty, but filled with data. apparently, nil is passed in func theApp()
Thank you in advance for your help 🙏

Global variable in swift 3

I have the variable provincias_ws which has the answer of a service, so far everything is fine but I want to know how I make the variable provincias_ws is global since I want to print it in the textFieldDidBeginEditing method
class ViewCtrl: UIViewController, ValidationDelegate, UITextFieldDelegate {
#IBOutlet weak var txt_ciudadU: SearchTextField!
override func viewDidLoad() {
super.viewDidLoad()
opciones_autocompletado()
txt_ciudadU.delegate = self
}
func opciones_autocompletado(){
txt_provinciaU.inlineMode = true
txt_ciudadU.inlineMode = true
Services.objServices.ServProv().then{
data -> Void in
let res = JSON(data)
let status = res["status"].boolValue
if(status){
let provincias_ws = res["data"]["provincias"] //How to make this variable global
}else{
let error = res["error"]["error_data"].stringValue
print(error)
}
SVProgressHUD.dismiss()
UIApplication.shared.endIgnoringInteractionEvents()
}.catch{
error -> Void in
SVProgressHUD.dismiss()
UIApplication.shared.endIgnoringInteractionEvents()
}
}
func textFieldDidBeginEditing(_ textField: UITextField) {
print(provincias_ws)
}
}
Thank you in advance
One can create a "global" variable (meaning it can be access from anywhere in the project) by defining it outside of the class block.
However, it looks like what you want is a class instance variable.
Your text field txt_ciudadU is already an example of an instance variable or property of the class ViewCtrl.
This would be the same, just without #IBOutlet weak.
So would probably be (assuming it's a String):
var provincias_ws: String?
Note the ? declares it as an optional value because it will not be set until it gets set inside your block. You could also initialize it to some default or empty value (e.g. var provincias_ws = "") or you could use ! to indicate you won't be trying to unwrap it when it's nil, although it's better not to rely on that because it is error-prone. What you choose to do depends on your needs.
And you would put that at the same hierarchical level as your text field variable (i.e. right above or below it, although it could go anywhere really, as long as it's not inside a function or another block).
And you would get rid of the current let next to provincias_ws and prepend it with self. i.e.:
self.provincias_ws = res["data"]["provincias"]
The self. is necessary because it's inside a block and the compiler needs that for context.

getting an error - use of self in property access'style' before self.init initializes self

private extension UIAlertAction {
convenience init(title: String?,
preferredStyle: UIAlertActionStyle, buttonIndex:Int,
tapBlock:((UIAlertAction,Int) -> Void)?) {
self.init(title: title, style: style) {
(action:UIAlertAction) in
if let block = tapBlock {
block(action,buttonIndex)
}
}
}
It's exactly what is says on the tin: You can't use a property before its initialized.
You're passing the value style to the designated initializer, which sets the value of style. Perhaps you meant to passpreferredStyle` instead?

Cannot assign value of type ViewController to type UITabBarControllerDelegate?

I have spent a few hours on this one, as I thought first that it was again an issue while migrating to swift2 with AnyObject versus PFObject. But apparently not! Here is the code:
class TimelineViewController: UIViewController, TimelineComponentTarget {
var timelineComponent: TimelineComponent<Post, TimelineViewController>!
override func viewDidLoad() {
super.viewDidLoad()
timelineComponent = TimelineComponent(target: self)
self.tabBarController?.delegate = self
}
Xcode returns on the 'self' of the last line:
Cannot assign value of type 'TimelineViewController' to type 'UITabBarControllerDelegate?'
Any help would be really appreciated, I would love to finally compile my code :)
Thanks!
I understood from the error message that I was trying to assign self as the delegate of a UITabBarControllerDelegate.
Or the TimeLineViewController is not a UITabBarControllerDelegate. I have therefore adopt this protocol.
class TimelineViewController: UIViewController, TimelineComponentTarget, UITabBarControllerDelegate {
It now works properly. Thanks!