What is the equivalent method in iOS for setImageNamed - ios10

What is the equivalent method for setImagedNamed for iPhone development. I understand this is an api for watch kit but what is the equivalent method for the iPhone. Thank you for your help with this.
#IBAction func rockChosen(_ sender: AnyObject) {
var randomNumber = Int(arc4random_uniform(3))
watchChoice.image = UIImage(options[randomNumber])
It is now giving me an error of Cannot subscript a value of type 'inout [String]' (aka 'inout Array')
How can I correct this.

watchChoice.image = UIImage(named: options[randomNumber])

Related

How do I cast an __NSMallocBlock__ to its underlying type in Swift 3?

I had a trick to help test UIAlertController that worked in Swift 2.x:
extension UIAlertController {
typealias AlertHandler = #convention(block) (UIAlertAction) -> Void
func tapButtonAtIndex(index: Int) {
let block = actions[index].valueForKey("handler")
let handler = unsafeBitCast(block, AlertHandler.self)
handler(actions[index])
}
}
This fails under Swift 3.x with fatal error: can't unsafeBitCast between types of different sizes, which tempts me to believe there might be a way to make the cast work. Can anyone figure it out?
Found a solution that works in Swift 3.0.1
extension UIAlertController {
typealias AlertHandler = #convention(block) (UIAlertAction) -> Void
func tapButton(atIndex index: Int) {
if let block = actions[index].value(forKey: "handler") {
let blockPtr = UnsafeRawPointer(Unmanaged<AnyObject>.passUnretained(block as AnyObject).toOpaque())
let handler = unsafeBitCast(blockPtr, to: AlertHandler.self)
handler(actions[index])
}
}
}
(Originally, the block value was the actual block, not a pointer to the block—which you obviously can't cast to a pointer to AlertHandler)
My answer is based on #Robert Atkins's, but shorter.
The problem here is that, valueForKey returns a Any typed object, and because in Swift,
MemoryLayout<Any>.size == 32
MemoryLayout<AnyObjcBlockType>.size == 8
an assertion will be triggered in unsafeBitCast when casting between types of different sizes.
One work-around is to create an intermediate wrapper and transform back to raw pointer, which satisfies MemoryLayout<UnsafeRawPointer>.size == 8.
A much simpler way is to create an indirect reference directly using protocol AnyObject, relying on the fact that MemoryLayout<AnyObject >.size == 8, we can write following valid code:
typealias AlertHandler = #convention(block) (UIAlertAction) -> Void
func tapButton(atIndex index: Int) {
if let block = actions[index].value(forKey: "handler") {
let handler = unsafeBitCast(block as AnyObject, to: AlertHandler.self)
handler(actions[index])
}
}
If your UIAlertController is an action sheet you can modify Robert's answer to dismiss the UIAlertController before you executed the handler.
dismiss(animated: true, completion: {() in handler(self.actions[index])})
I was using this extension for testing and without this modification my assertions for presented view controller were failing.

Cannot call value of non-function type 'AVAudioPlayer'

I keep looking over my cade and can't seem to find what the problem is. Here is the code.
#IBAction func PlayAudio(sender: AnyObject) {
let AVAudioPlayer = ButtonAudioPlayer{
ButtonAudioPlayer.play()
}
It just throws out the error that says "Cannot call value of non-function type 'AVAudioPlayer'"
You are trying to use AVAudioPlayer (capitalized) as a constant name. That name is already defined as a class.
Make it more like:
let myAudioPlayer = ButtonAudioPlayer{
ButtonAudioPlayer.play()
// etc.
Perhaps you meant something like this?
let audioPlayer: AVAudioPlayer = ButtonAudioPlayer{

Swift: Change object with property in array

I am trying to amend an object in my AnyObject (savedProgram). I want to save Taking the advice from another thread, I've used 'as!' to convert it to [AnyObject]:
#IBAction func saveM(sender: UIButton) {
brain.variableValues["M"] = displayValue
savedProgram = brain.program as! [AnyObject]
var indexValue = savedProgram?.indexOfObject("M")
savedProgram?[indexValue!] = displayValue
It complains at the 4th row. I've also tried changing the original variable savedProgram to [AnyObject] but then I am not sure how to look up "M" and amend it.
Thanks for your help.

Hi Im getting the swift error = Value of type 'UITableView' has no member 'labelText'

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "Share" {
var destViewController = (segue.destinationViewController as! UITableViewController).tableView //Warning
destViewController.labelText = TEXT.text!
//Error = Value of type 'UITableView' has no member 'labelText'
}
The reason that you are getting that error is pretty self-explanatory: there is no defined variable "labelText" in the UITableViewController class. What exactly are you trying to accomplish with this code? It is very ambiguous. If you were more clear about what you were trying to do, providing an answer for you would be much easier.
I think you want to use textLabel instead of labelText

Conversion to Swift 2 NSIndexPath error

Problem: Upon converting to Swift 2 I get the following error: "Value of optional type [NSIndexPath]? not unwrapped, did you mean to use "!" or "?"". The issue is that if I use '?', it gives an error saying I should use '!', and if I use '!' it gives an error saying I should use '?'. Thus it creates this nasty little bug loop that seems to be unfixable.
Code:
override func shouldPerformSegueWithIdentifier(identifier: String, sender: AnyObject?) -> Bool {
if identifier == Constants.SegueIdentifier {
if let selectedRowIndex = collectionView?.indexPathsForSelectedItems().last as? NSIndexPath {
if let cell = collectionView?.cellForItemAtIndexPath(selectedRowIndex) {
//We check if the selected Card is the one in the middle to open the chat. If it's not, we scroll to the side card selected.
if cell.frame.size.height > cell.bounds.size.height {
return true
} else {
collectionView?.scrollToItemAtIndexPath(selectedRowIndex, atScrollPosition: UICollectionViewScrollPosition.CenteredHorizontally, animated: true)
return false
}
}
}
}
return true
}
I haven't been able to come up with any work arounds since it seems like I need to somehow unwrap it. Has anyone seen this problem?
indexPathsForSelectedItems() returns [NSIndexPath]? (optional), you have to add another question mark for optional chaining and remove as? NSIndexPath as the compiler knows the unwrapped type.
if let selectedRowIndex = collectionView?.indexPathsForSelectedItems()?.last {
Solution:
if let selectedRowIndex = collectionView!.indexPathsForSelectedItems()!.last! as? NSIndexPath
My only concern is type safety but it works in my project as it currently is.