How can I correct editor.photoEditorDelegate = self? - ios

I am still a beginner at Swift programming and I am trying to build a very basic iOS app. The app requires users to take a photo and edit them on a photo editor. I am having issues with using editor.photoEditorDelegate = self.
This is what the developer of the photo editor used in his example and it gave no problem but it's not working for me. It is giving an error of:
Cannot assign value of type 'PhotosViewController?' to type 'PhotoEditorDelegate?'
I have tried to fix it with:
editor.photoEditorDelegate = self as? PhotoEditorDelegate
but it just makes the app crash when the editor is called.
I declared the editor with:
let editor = PhotoEditorViewController(nibName:"PhotoEditorViewController",bundle: Bundle(for: PhotoEditorViewController.self))

The error is pretty self explanatory ! You need to add this delegate to your class name "PhotoEditorDelegate"
This is a sample code based on the information you have provided
class PhotosViewController: PhotoEditorDelegate {
override func viewDidLoad() {
super.viewDidLoad()
let editor = PhotoEditorViewController(nibName:"PhotoEditorViewController",bundle: Bundle(for: PhotoEditorViewController.self))
editor.photoEditorDelegate = self
// Do any additional setup after loading the view.
}
}

You need to make your PhotosViewController your PhotoEditorDelegate:
class PhotosViewController: PhotoEditorDelegate {
...

Related

Instance member cannot be used on type 'ViewController' in Xcode 9 [duplicate]

class ViewController: UIViewController {
let fortuneArray = ["You will find true love in the summer.", "Outlook not good", "You may find great success in business soon.", "Watch out for grey cats."]
let randomIndex = Int(arc4random_uniform(fortuneArray.count))
override func viewDidLoad() {
super.viewDidLoad()
let randomIndex = Int(arc4random_uniform(UInt32(fortuneArray.count)))
print("random index: ")
print(randomIndex)
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// actions
#IBAction func cookiePressed(sender: AnyObject) {
fortune.text = fortuneArray[randomIndex]
}
I'm creating a very simple fortune telling app in Swift and I keep running into issues with arc4random_uniform. Currently I'm just trying to get the app to draw a string at random but I get an error saying:
Instance member 'fortuneArray' cannot be used on type 'ViewController'
on the line where I am declaring the variable randomIndex. I've been using google for awhile but haven't found a fix. Hopefully someone can help, thanks!
* Update *
Problem solved! Thanks.
If the code you pasted is not defined in a method like viewDidLoad, you cannot use a variable thats defined at the class level for another variable thats defined at the class level as well. These variables are determined at run time and the order they are determined is not known so fortuneArray may not exist before randomIndex is made (might not really work like this behind the scenes but you can think of it this way at least)
you should compute these variables inside viewDidLoad or init or some other function instead
Ah, I figured it out with Fonix's help. I declared the random number in an IBAction and that took care of it.

How do I pass a configuration to KinWebBrowserViewController?

I'm trying to find the correct syntax everywhere and I'm just not understanding it.
How do I pass a configuration to KinWebBrowserViewController?
Here's the KinWebBrowserViewController code converted to Swift (I can also provide the original Objective-C)
/*
Initialize a basic KINWebBrowserViewController instance for push onto navigation stack
Ideal for use with UINavigationController pushViewController:animated: or initWithRootViewController:
Optionally specify KINWebBrowser options or WKWebConfiguration
*/
class func webBrowser() -> KINWebBrowserViewController? {
}
class func webBrowser(with configuration: WKWebViewConfiguration?) -> KINWebBrowserViewController? {
}
And here's where the browser is instantiated:
enum WebviewViewControllerFactory {
static func make(for url: String) -> WebBrowserViewController {
let webBrowser = WebBrowserViewController()
webBrowser.showsURLInNavigationBar = false;
webBrowser.showsPageTitleInNavigationBar = false;
webBrowser.barTintColor = UIColor.navBackground
webBrowser.loadURLString(url);
return webBrowser;
}
}
All I want to do is add the following configuration:
let config = WKWebViewConfiguration()
config.userContentController.add(self, name: "callbackHandler")
And add this configuration to the browser.
This seems like it should be easy, but I have been fighting with Swift syntax for hours trying to get what I feel should be fairly simple to work. I've tried initializers, I've tried additional subclassing, I've tried even discarding this and using WkWebView.
How do I set a single configuration on KinWebBrowser?

Instance member cannot be used on type 'ViewController'

class ViewController: UIViewController {
let fortuneArray = ["You will find true love in the summer.", "Outlook not good", "You may find great success in business soon.", "Watch out for grey cats."]
let randomIndex = Int(arc4random_uniform(fortuneArray.count))
override func viewDidLoad() {
super.viewDidLoad()
let randomIndex = Int(arc4random_uniform(UInt32(fortuneArray.count)))
print("random index: ")
print(randomIndex)
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// actions
#IBAction func cookiePressed(sender: AnyObject) {
fortune.text = fortuneArray[randomIndex]
}
I'm creating a very simple fortune telling app in Swift and I keep running into issues with arc4random_uniform. Currently I'm just trying to get the app to draw a string at random but I get an error saying:
Instance member 'fortuneArray' cannot be used on type 'ViewController'
on the line where I am declaring the variable randomIndex. I've been using google for awhile but haven't found a fix. Hopefully someone can help, thanks!
* Update *
Problem solved! Thanks.
If the code you pasted is not defined in a method like viewDidLoad, you cannot use a variable thats defined at the class level for another variable thats defined at the class level as well. These variables are determined at run time and the order they are determined is not known so fortuneArray may not exist before randomIndex is made (might not really work like this behind the scenes but you can think of it this way at least)
you should compute these variables inside viewDidLoad or init or some other function instead
Ah, I figured it out with Fonix's help. I declared the random number in an IBAction and that took care of it.

VKSdk how to set delegate in Swift?

I'm trying to make VKSdk work with Swift. Documentation says that I need to set delegate and in order to do this I have to do the following in Objective-c
VKSdk *sdkInstance = [VKSdk initializeWithAppId:YOUR_APP_ID];
[sdkInstance registerDelegate:delegate];
[sdkInstance setUiDelegate:uiDelegate];
In swift I try to do it like this
override func viewDidLoad() {
super.viewDidLoad()
VKSdk.initializeWithAppId("1111111")
VKSdk.registerDelegate(self)
}
But it says Cannot convert value of type "ViewController" to expected argument type "VKSdk"
How to do this correctly ?
Working code below
let sdk = VKSdk.initializeWithAppId("111111")
sdk.registerDelegate(self)

swift check an object's property existence

I have the below UINib extension method, I was wondering if I can set a delegate for the unarchived view
public class func decodeView<T:UIView>(nibName name:String,className classType:T.Type,delegate:AnyObject) -> T {
let nib = UINib(nibName: name)
let topLevelObjects = nib.instantiateWithOwner(nil, options: nil)
let view = topLevelObjects[0] as T
view.setTranslatesAutoresizingMaskIntoConstraints(false)
//check if view.delegate exists then view.delegate = delegate
return view
}
If you're asking if Swift supports reflection, TL;DR: you need to subclass from NSObject. Else you get limited info.
In this question, Does Swift support reflection? you get a more detailed discussion about the possibilities you have.
Once you have this part cleared, an example of how to obtain a list of properties can be found in this SO Answer
Although a quick & dirty way could be just to try and access the property (using KVC) and catch the exception if it fails. Swift does NOT support Try/Catch/Finally constructs, but this nice hack allows you to write code like:
SwiftTryCatch.try({
// try something
}, catch: { (error) in
println("\(error.description)")
}, finally: {
// close resources
})

Resources