VKSdk how to set delegate in Swift? - ios

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)

Related

Get Identifier and Hint of UIView in swift class

I'm assigning an identifier to a view using storyboard and want to access it through swift class. Is there any method to do that? I'm searching it on stackoverflow but couldn't find any solution.
Few people gave solution to use tag instead of identifier but I want to ask that isn't there any solution to get these hints and identifiers inside of swift class.
it gives error when ever I try to access it using accessibilityIdentifier.
You access it via accessibilityIdentifier e.g. myView.accessibilityIdentifier
The error is because you are not using the value. Just assign it to a variable an it will work.
let yourId = yourView.accessibilityIdentifier
Here is an extension
var viewIdentifier: String?{
get {
return self.accessibilityIdentifier
}
set {
self.accessibilityIdentifier = newValue
}
}

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 can I correct editor.photoEditorDelegate = self?

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 {
...

canBecomeFocused on swift 3

I'm trying to create a custom View which I need to focus.
According to numerous Sources this is the way to go :
override func canBecomeFocused() -> Bool {
return true
}
However after converting to Swift 3.0 this no longer works.
It throws the error:
Method does not override any method from its superclass.
If i remove the override another error gets thrown:
Method 'canbecomeFocused()' with Objective-C selector 'canBecomeFocused' conflicts with better for 'canBecomeFocused' from superclass 'UIView' with the same Objective-C Selector.
Anyway I can make a UIView Selectable for TvOS?
In Swift 3 it is change to Instance Property, so try like this.
override var canBecomeFocused: Bool {
return true
}
Check Apple Documentation on canBecomeFocused for more details.

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.

Resources