Cannot call value of non-function type 'AVAudioPlayer' - swift

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{

Related

Swift If you specify a non-nil format dictionary in your settings, your delegate must respond to the selector captureOutput:didFinishProcessingPhoto

The code I am using below is supposed to take a photo and then convert the image to base64 in order to send it to a server. The code worked taking the photo, converting it to base64, and uploading it to my server but has stopped working. I have tried using other stack overflow posts to solve this issue but it has not worked for me. Thanks for any responses in advance!
Error
Thread 1: Exception: "*** -[AVCapturePhotoOutput capturePhotoWithSettings:delegate:] If you specify a non-nil format dictionary in your settings, your delegate must respond to the selector captureOutput:didFinishProcessingPhoto:error:, or the deprecated captureOutput:didFinishProcessingPhotoSampleBuffer:previewPhotoSampleBuffer:resolvedSettings:bracketSettings:error:"
Code:
#IBAction func takePhotoButtonPressed(_ sender: Any) {
let settings = AVCapturePhotoSettings()
let previewPixelType = settings.availablePreviewPhotoPixelFormatTypes.first!
let previewFormat = [kCVPixelBufferPixelFormatTypeKey as String: previewPixelType,
kCVPixelBufferWidthKey as String: 160,
kCVPixelBufferHeightKey as String: 160]
settings.previewPhotoFormat = previewFormat
sessionOutput.capturePhoto(with: settings, delegate: self)
}
func photoOutput(_ output: AVCapturePhotoOutput, didFinishProcessingPhoto photo: AVCapturePhoto, error: Error?) {
let imageData = photo.fileDataRepresentation()
let base64String = imageData?.base64EncodedString()
print(base64String!)
}
Let's break down the error message:
Thread 1: Exception:
An Objective C exception was thrown from Thread 1 of your program (that's probably the main thread). Not particularly interesting/insightful.
-[AVCapturePhotoOutput capturePhotoWithSettings:delegate:]
This syntax describes the Objective-C method that threw the exception. The method's selector is capturePhotoWithSettings:delegate:, and it belongs to the AVCapturePhotoOutput class. The - indicates that it's an instance method (where a + would have indicated it's a class method).
If you specify a non-nil format dictionary in your settings ...`
This is the case, you called capturePhoto(with: settings, ... with settings that aren't nil.
your delegate must respond to the selector captureOutput:didFinishProcessingPhoto:error:
The system is complaining that you passed a delegate that does not respond to the selector captureOutput:didFinishProcessingPhoto:error: (In Swift, the method is imported as photoOutput(_:didFinishProcessingPhoto:error:)).
That is, your delegate doesn't define any methods with that name. You decided to pass self (whatever that is, I don't know without context) as the delegate: capturePhoto(..., delegate: self).
Whatever the type of self is, it already conforms to the AVCapturePhotoCaptureDelegate protocol (or else this would never have compiled). But it does not implement this method, which is optional by the protocol, but mandatory in this context.
or the deprecated captureOutput:didFinishProcessingPhotoSampleBuffer:previewPhotoSampleBuffer:resolvedSettings:bracketSettings:error:
This is just telling you that instead of captureOutput:didFinishProcessingPhoto:error:, you could instead fix this issue by implementing captureOutput:didFinishProcessingPhotoSampleBuffer:previewPhotoSampleBuffer:resolvedSettings:bracketSettings:error:, but since that's deprecated, you probably shouldn't use it.
So in all, whatever the type of self is, you need to make sure it implements the method photoOutput(_:didFinishProcessingPhoto:error:)

Can't cast from RLMObject to a subclass of Object?

Song is a subclass of RLMObject (typealias Object) and is used throughout my app, including in searchViewController(_:cellForObject:atIndexPath:)
let song = object as! Song
But in my prepare(for segue:) method (below), when I try to perform the same downcast, the compiler says "Cast from 'RLMObject' to unrelated type 'Song' always fails."
if let row = tableView.indexPathForSelectedRow?.row {
YpbApp.currentRequest?.songObject = results!.object(at: UInt(row)) as? Song
}
This doesn't makes sense, what's wrong here?
RLMObject is not a typealias for Object; they are different classes entirely that have different interfaces. It sounds like you are trying to mix the Swift and Objective-C APIs, which is not supported.

What is the equivalent method in iOS for setImageNamed

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])

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.

Custom NSError weird behaviour Swift

In Swift, I have a custom NSError, I need to get the error userInfo dictionary and add things later, but it is nil in the assign line, but then error.userInfo have an object...
With error.userInfo as nil:
class MyError: NSError {
init(error: NSError) {
var newUserInfo = error.userInfo
...newUserInfo is nil...
super.init(...)
}
}
If I assign it 2 times it works ( I know there's something missing but what?)
init(error: NSError) {
var newUserInfo = error.userInfo
newUserInfo = error.userInfo
...newUserInfo now contains a dictionary...
}
Why?
This looks maybe compiler bug-ey to me, but it's hard to tell without seeing more of your code. At any rate, this sort of thing is easier to debug if you use conditional cast. userInfo in swift is a Dictionary<NSObject: AnyObject>?; if you're getting this from a Cocoa API you can do something like:
if let userInfo = error.userInfo as? [NSObject: NSObject] {
// modify and assign values as necessary
}
this will at least make it clearer where things are breaking.