Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 433 Vote(s) - 3.48 Average
  • 1
  • 2
  • 3
  • 4
  • 5
'NSLog' is unavailable: Variadic function is unavailable in swift

#1
I'm new to swift. when I'm learning just basics I got this error at `NSLog`

**Here is my code :**

import UIKit

class ViewController: UIViewController {

var myString: NSString?

override func viewDidLoad() {
super.viewDidLoad()
myString = "himanth"
print(myString)
NSLog("%@" , myString)
// 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.
}


}

If I am declaring `myString` like this

var myString: NSString!

It's fine to use `NSLog` and I can able to see console as well.

But declaring string like this causing problem

var myString: NSString?

It reflects at `NSLog` and showing error.

What is the problem with that?


Reply

#2
`NSLog` does not support any types except `String`. You cannot pass, i.e. your custom struct / enum instance as NSLog variadic argument.

Instead use `description` variable to show up necessary details about your instance. Your class / struct / enum could conform `CustomStringConvertible` to unify the way to access `description` variable as well.
Reply

#3
If you declare `var myString: NSString?` as an optional then you need to make sure it has a value before you pass it to the `NSLog`.

So you could do it like this then `NSLog("%@" , myString!)`. If `myString` is nil and you put `!` the program will crash and you will get

fatal error: unexpectedly found nil while unwrapping an Optional value.

But if it has a value the program will continue as normal and print out

2016-10-03 10:26:25.077 Swift3.0[65214:1579363] Test

I wrote `myString = "Test"`.

Another example as [@Grimxn][1] mentioned is the coalescing operator.

NSLog("%@", myString ?? "<nil>")


[1]:

[To see links please register here]

Reply

#4
Have the same problem, accepted answer doesn't help.

func handleSomeResp(_ response: DataResponse<Any>) {
NSLog("handle error: %@", response)
}

as you see, there is nothing about optionals.
I guess it because of `<Any>`, and any type, unclear for NSLog, watch similar problem [here][1]: .
Edit this answer if you have a solution to parse generics for NSLog.


[1]:

[To see links please register here]

Reply

#5
`NSLog()` cannot print Swift Optionals.


let optional: String?
NSLog("%@", optional) // 'NSLog' is unavailable: Variadic function is unavailable

let nonOptional: String
NSLog("%@", nonOptional) // Ok!

NSLog("%@", optional ?? "value-if-nil") // Ok!

Fix by passing a non-optional value to `NSLog()` instead.

NOTE:

`print()` ***can*** print Swift Optionals.
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through