Cannot Convert Value of Type 'String.Type' to Expected Argument Type 'String!'

Cannot convert value of type 'String.Type' to expected argument type 'String!'

Lets walk through what deferredWampCRASigningBlock is:

((String!, ((String!) -> Void)!) -> Void)!

This is a void function that takes two things:

  • a String!
  • a void function that takes a String!

So when you call it, you must pass it those things. A string and a function.

let challenge = "challenge"
let finishBLock: String! -> Void = { signature in }
self.wamp?.config.deferredWampCRASigningBlock?(challenge, finishBlock)

From some of your comments, you seem to not know what challenge should be at this point. That suggests you should not be calling this function. This function is intended to be called by the part of the program that does know challenge is.

The confusion may be related to "when I try to instantiate it." The code you've given doesn't instantiate anything. It is trying to call the function. Perhaps what you really meant was to create the function and assign it:

self.wamp?.config.deferredWampCRASigningBlock = 
{ (challenge: String!, finishBlock: ((String!) -> Void)!) -> Void in
// ...
}

Cannot convert value of type 'String?.Type' to expected argument type 'String?'

Use This

var updater = productsList(id: updaterId, p_name: nil, image: nil, audio: nil)

Instead of

var updater = productsList(id: String?, p_name: String?, image: String?, audio: String?)

Cannot convert value of type 'String' to expected argument type 'Data'

The from parameter of decode must be Data, not String, this is the message of the error. The literal date string as JSON data makes no sense anyway. It's impossible to compose variable names at runtime.

What you can do is

let month = Calendar.current.component(.month, from: Date())

let data : Data
switch month {
case 1: data = Data(JanMoonData.utf8)
case 2: data = Data(FebMoonData.utf8)
// ...
}

let moonphase = try! JSONDecoder().decode([MoonPhase].self, from: data)

or

let month = Calendar.current.component(.month, from: Date())

let moonJSON : String
switch month {
case 1: moonJSON = JanMoonData
case 2: moonJSON = FebMoonData
// ...
}

let moonphase = try! JSONDecoder().decode([MoonPhase].self, from: Data(moonJSON.utf8)

or

let moonJSONArray = [JanMoonData, FebMoonData, MarMoonData, ..., DecMoonData]
let month = Calendar.current.component(.month, from: Date())
let moonJSON = moonJSONArray[month - 1]
let moonphase = try! JSONDecoder().decode([MoonPhase].self, from: Data(moonJSON.utf8)

Cannot convert value of type 'String' to expected argument type 'Category'

You're passing in a string to the initialiser of CategoryView(categories: category.title). Remove the .title at the end and pass in the category CategoryView(categories: category).

As for the navigation title, the modifier needs to go inside the NavigationView and not attached to the NavigationView itself. This is because you can push multiple views onto one NavigationView with different Navigation Bar Titles.

NavigationView {
List {
ForEach(questions) { question in
Text("Question \(question.id)")
}
} // end List
.navigationBarTitle("Welcome")
} // end NavView

Cannot convert value of type '()' to expected argument type 'String'

Your problem begins with this line:

let location = getLocationFromPostalCode(postalCode: zipCode)

The problem is that your getLocationFromPostalCode has no return value so the compiler thinks that the implicit type of location is () which means a function with no return (void) type.

So, in theory, you would want to change:

func getLocationFromPostalCode(postalCode : String) {

to:

func getLocationFromPostalCode(postalCode : String) -> String {

and have the function return a String value.

But, you can't do that either since the implementation of your getLocationFromPostalCode consists of getting the location from an asynchronous network call.

The proper solution is to rewrite that function to take a completion handler that returns the obtained location.

func getLocationFromPostalCode(postalCode: String, completion: (String?) -> Void) {
let geocoder = CLGeocoder()

geocoder.geocodeAddressString(postalCode) { (placemarks, error) -> Void in
// Placemarks is an optional array of type CLPlacemarks, first item in array is best guess of Address

if let placemark = placemarks?.first {
if placemark.postalCode == postalCode {
// you can get all the details of place here
print("\(placemark.locality)")
print("\(placemark.country)")
completion(placemark.locality) // or whatever value you want
return
}
else{
print("Please enter valid zipcode")
}
}

completion(nil) // no location found
}
}

Now, with that all fixed up, you need to redo how you get the location.

@IBAction func completeButtonAction(_ sender: Any) {
var pictureD: Data? = nil
if let imageView = self.sentPic.image {
pictureD = UIImageJPEGRepresentation(imageView, 0.70)
}

let emailField = email.lowercased()
let finalEmail = emailField.trimmingCharacters(in: .whitespacesAndNewlines)
let biography = bioTextView.text!
let passwordText = password

if finalEmail.isEmpty || biography.isEmpty || password.isEmpty || pictureD == nil {
self.view.endEditing(true)
let alertController = UIAlertController(title: "OOPS", message: " You must fill all the fields", preferredStyle: .alert)
alertController.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
present(alertController, animated: true, completion: nil)
}else {
getLocationFromPostalCode(postalCode: zipCode) { (location) in
guard let location = location else {
print("no location")
return
}

let nameText = name
let interests = options.joined(separator: " , ")

SVProgressHUD.show()

self.view.endEditing(true)
authService.signUP(firstLastName: nameText, email: finalEmail, location: location, biography: biography, password: password, interests: interests, pictureData: pictureD!)
SVProgressHUD.dismiss()
}
}

let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "NewTabBarViewController") as! UIViewController
// Alternative way to present the new view controller
self.navigationController?.present(vc, animated: true, completion: nil)
}

SharingMusicPlayer.swift:12:79: Cannot convert value of type 'AVAudioPlayer.Type' to expected argument type 'AVAudioPlayer'

This is what I came up with, though I am still not sure if this is the right way to do a singleton in swift:

class SharingMusicPlayer {
static var sharingMusicPlayer = SharingMusicPlayer()
var backgroundMusicPlayer : AVAudioPlayer?

private init() {

}

func playBackgroundMusic(filename: String) {
let url = Bundle.main.url(
forResource: filename, withExtension: nil)
if (url == nil) {
print("Could not find file: \(filename)")
return
}
do {
try self.backgroundMusicPlayer = AVAudioPlayer(contentsOf: url!)
} catch {
print("Could not create audio player")
}


backgroundMusicPlayer!.numberOfLoops = -1
backgroundMusicPlayer!.prepareToPlay()
backgroundMusicPlayer!.play()
}

func stopBackgroundMusic(filename: String) {
self.backgroundMusicPlayer!.stop()
}
}

Then this is how I would call the functions to start and stop the music:

override func touchesBegan(_ touches: Set<UITouch>,
with event: UIEvent?) {
/* Called when a touch begins */
for touch: AnyObject in touches {
//1
let location = touch.location(in:self)
//2
let theNode = self.atPoint(location)

//...

if theNode.name == playMusicButton!.name {
print("The \(playMusicButton!.name!) is touched ")
SharingMusicPlayer.sharingMusicPlayer.playBackgroundMusic(
filename: "BeethovenPianoSonataNr15InDmajorOp28Pastoral.mp3")
}

if theNode.name == stopMusicButton!.name {
print("The \(stopMusicButton!.name!) is touched ")
SharingMusicPlayer.sharingMusicPlayer.stopBackgroundMusic()
}
}

Please note that the above buttons are in SpriteKit so they are different than the usual UIButton objects in Main.storyboard.

Cannot convert value of type 'Int' to expected argument type 'Double'

totalWithTax is a Double. numPeople is an Int.

You need to convert numPeople to a Double too.

return totalWithTax / Double(numPeople)

Operators like / don't work with mismatching types.



Related Topics



Leave a reply



Submit