Casting in swift - swift

So my swift skills arent great... I want to try and retrieve an audio track description for a video and have come up with this method. If anything fails the method should return null
func getAudioTrackDescription(path: String) -> AudioStreamBasicDescription? {
let asset = getAssetFromPath(path: path)
guard let track = asset.tracks(withMediaType: AVMediaType.audio).first else {
return nil
}
guard let audioDesc = track.formatDescriptions.first else {
return nil
}
// let casted = audioDesc as! CMAudioFormatDescription
// THE SDF IS JUST AN EXAMPLE THAT CAUSES IT TO FAIL
// I ACTUALLY WANT TO USE THE COMMENTED LINE ABOVE
if let casted = ("Sdf" as! CMAudioFormatDescription) {
let basic = CMAudioFormatDescriptionGetStreamBasicDescription(casted)
guard let pointee = basic?.pointee else {
return nil
}
return pointee
} else {
return nil
}
}
I am really struggling to understand how the casting works in swift...
The track.formatDescriptions list holds type Any which means it could crash if the type is not what i expect it to be (CMAudioFormatDescription)
For example I have changed the if audioDesc to be the string "Sdf" and the code crashes.
I want to be able to check if audioDesc can be casted to CMAudioFormatDescription and if cant i want to return nil.
I tried using as? and this always gives me this error
Conditional downcast to CoreFoundation type 'CMAudioFormatDescription' (aka 'CMFormatDescription') will always succeed
Can someone help?
---------------------- EDIT
is below a safe approach?
func getVideoAudioChannelCount(path: String) -> Int {
guard let audioFile = getAudioInformation(path: path) else {
return DEFAULT_AUDIO_CHANNEL
}
return Int(audioFile.channelCount)
}
func getAudioInformation(path: String) -> AVAudioFormat? {
var returnVar: AVAudioFormat?;
do {
returnVar = try AVAudioFile(forReading: URL(fileURLWithPath: path)).fileFormat
} catch _ {
returnVar = nil
}
return returnVar
}

According to the documentation, the array will always contain CMFormatDescription (aka CMAudioFormatDescription), so you can safely cast with as!.
The array contains CMFormatDescriptions (see CMFormatDescription), each of which indicates the format of media samples referenced by the track.
You get the error that conditional casts always succeed because CMAudioFormatDescription is a Core Foundation type (it conforms to _CFObject). For more info, see here.
Edit:
is below a safe approach?
Yes, if by "safe" you mean it won't crash. You can simplify the code if you use try?:
func getAudioInformation(path: String) -> AVAudioFormat? {
try? AVAudioFile(forReading: URL(fileURLWithPath: path)).fileFormat
}

Related

Swift cast if possible

I have this code
let jsonData = try JSONSerialization.jsonObject(with: data, options: []) as! [Any?]
if var first = jsonData[0] as! String?{
if(first=="Error"){
DispatchQueue.main.async(execute: {
self.postNotFoundLabel.isHidden = false
});
}else if(first=="Empty"){
print("Empty")
}
}
What i want to do is to cast jsonData[0] to String if it's possible and if it's not then move on.But instead when it's not possible application stops and gives me an error
Could not cast value of type '__NSDictionaryI' (0x1092054d8) to 'NSString' (0x108644508).
How can i cast only when it's possible?
You are trying to force-cast to an optional String. That's not what you want.
Change:
if var first = jsonData[0] as! String? {
to:
if var first = jsonData[0] as? String {
This tries to cast to String. If jsonData[0] isn't actually a String, you get nil and the if var fails.
And you probably want if let, not if var since you don't seem to be making any change to first.
First of all JSON objects will never return optional values so [Any?] is nonsense.
Second of all the error message says the type cast to string is inappropriate because the type of the result is actually a dictionary.
Solution: Check the type for both String and Dictionary
if let jsonData = try JSONSerialization.jsonObject(with: data) as? [Any],
let first = jsonData.first {
if let firstIsDictionary = first as? [String:Any] {
// handle case dictionary
} else if let firstIsString = first as? String {
// handle case string
}
}
PS: A type cast forced unwrap optional to optional (as! String?) is nonsense, too.
Here's the Swifty way to do what you're doing :)
guard let jsonData = try JSONSerialization.jsonObject(with: data, options: []) as? [Any?], let first = jsonData[0] as? String else {
DispatchQueue.main.async(execute: {
self.postNotFoundLabel.isHidden = false
});
return
}
if(first == "Empty") {
print(first)
}
Don't use as! if you are not sure that casting will succeed. The exclamation mark after the as keyword forces the casting, which throws an error if the casting does not succeed.
Use as? instead, which returns an optional variable of the type you were trying to casting to. If the casting fails, instead of throwing an error, it just returns nil.
let jsonData = try JSONSerialization.jsonObject(with: data) as? [Any]
if var first = jsonData.first as? String{
if(first=="Error"){
DispatchQueue.main.async(execute: {
self.postNotFoundLabel.isHidden = false
});
}else if(first=="Empty"){
print("Empty")
}
}

Guard let construction, still getting fatal error: Index out of range

I am trying to parse some data with this code:
func findDate(data: String?) -> String {
guard let date: String? = (data!.componentsSeparatedByString("T"))[0] else{
return "20000101"
}
return date!
}
I tried the guard structure to prevent errors when there is no data found, or it has a different structure, but I still get the error when I run it:
fatal error: Index out of range
Does somebody know how to fix this?
The access of the element at index zero always happens. If the result of the call to components(separatedBy:) returns an empty array, your code crashes. Also, you should avoid force unwrapping data.
A solution to these crashes is to use the first property of the array, which is optional, so you can safely unwrap it.
guard let date = data?.components(separatedBy: "T").first else {
return "20000101"
}
return date.
Also, this could then be simplified using the nil coalescing operator:
return data?.components(separatedBy: "T").first ?? "20000101"
Please check out this code:
func findDate(data: String?) -> String {
guard let date: String? = (data?.componentsSeparatedByString("T"))?[0] else{
return "20000101"
}
return date!
}
findDate(nil)
findDate("")
findDate("98588T99")
There are two issues here:
1) Trying to access an index of an array that isn't large enough doesn't produce nil, it just crashes. For example, this code will crash:
let strings: [String] = []
guard let firstString = strings[0] else {
// couldn't find firstString
}
Instead, use the first method:
let strings: [String] = []
guard let firstString = strings.first else {
// couldn't find firstString
}
first attempts to access the first element of the array, and it returns nil if the array doesn't have a first element.
2) The purpose of guard let is to guarantee that the value you get out isn't nil. So your date value should be of type String, not String?, and you shouldn't have to force-unwrap it. For example:
let strings: [String] = ["one", "two", "three"]
guard let firstString = strings.first else {
// no first string
}
print(firstString) // firstString is not nil, no need to unwrap it

Optional in text view showing when printing

Hi all I have tried a few solutions but no luck.
I am getting the text from Data Core, but the textview has optional on it.
when it prints it shows optional in the text.
page22TextView?.text = ("\(trans.value(forKey: "page22"))")
can anyone shed light on this ! have tried to unwrap but it stillelow: shows.
the full function is below:
func getTranscriptions () {
//create a fetch request, telling it about the entity
let fetchRequest: NSFetchRequest<TextInputs> = TextInputs.fetchRequest()
do {
//go get the results
let searchResults = try getContext().fetch(fetchRequest)
//I like to check the size of the returned results!
print ("num of results = \(searchResults.count)")
//You need to convert to NSManagedObject to use 'for' loops
for trans in searchResults as [NSManagedObject] {
page22TextView?.text = ("\(trans.value(forKey: "page22"))")
//get the Key Value pairs (although there may be a better way to do that...
print("\(trans.value(forKey: "page22"))")
}
} catch {
print("Error with request: \(error)")
}
}
try to set default value of getting nil value
page22TextView?.text = (trans.value(forKey: "page22") as? String) ?? ""
It'll set your value from trans and if it retrun nill will be set by "".
Hope it'll help you.
try with if-let statement:
if let result = trans.value(forKey: "page22") {
page22TextView?.text = result
}
Or try with guard statement:
guard let result = trans.value(forKey: "page22") else { return }
page22TextView?.text = String(describing: result)
Or you can force upwrap it like:
let result = trans.value(forKey: "page22")
if result != nil {
page22TextView?.text = result! as! String
}
Or you can follow the way suggested by #MrugeshTank below in answers
try to unwrap optional using if let then assign to your textview (if necessary then downcast your value)
if let value = trans.value(forKey: "page22") {
page22TextView?.text = value
}
or
use guard for unwrap

Swift check type of object

I want to compare types of objects in swift.
I've got a function which takes an object of NSError as parameter. It should return a custom string.
It looks like this:
static func getLocalizedErrorText(error: NSError) -> String{
switch error {
case is NoConnection: //class NoConnection: NSError
return "....."
...
}
But the function is not working as expected. I think the main problem is that this example is not working:
var dummy = MySubError() //class MySubError: MyBaseError
var dummy2: MyBaseError?
dummy2 = MySubError()
if dummy.dynamicType == MySubError.self {
//This will work
}
if dummy2.dynamicType == MySubError.self {
//This will not work
}
How can I check which type the parameter got?
You can check for a type using
if error is MySubError {
// do stuff
}
You can also do an optional cast, which will succeed, if the type matches or return nil, if not:
let subError = error as? MySubError
which you can also use in a guard predicate or if let statement:
if let subError = error as? MySubError {
// do stuff
}
or
guard let subError = error as? MySuberror else { return }

Extra argument 'error' in call - do/catch?

I know there is new error handling i.e. do/catch but not sure if it applies here and even if it does it's pretty difficult for me even going through the documentation. Could someone show me the correct code block please.
/*** error Extra argument 'error' in call ***/
var plistDic = NSPropertyListSerialization.propertyListWithData(plistData!,
options:Int(NSPropertyListMutabilityOptions.MutableContainersAndLeaves.rawValue),
format: nil, error: &error) as Dictionary<String, Dictionary<String, String>>
assert(error == nil, "Can not read data from the plist")
return plistDic
}
// END
EDIT:
let YALCityName = "name"
let YALCityText = "text"
let YALCityPicture = "picture"
private let kCitiesSourcePlist = "Cities"
class YALCity: Equatable {
var name: String
var text: String
var image: UIImage
var identifier: String
// MARK: Class methods
class internal func defaultContent() -> Dictionary<String, Dictionary<String, String>> {
let path = NSBundle.mainBundle().pathForResource(kCitiesSourcePlist, ofType: "plist")
let plistData = NSData(contentsOfFile: path!)
assert(plistData != nil, "Source doesn't exist")
do {
let plistDic = try NSPropertyListSerialization.propertyListWithData(plistData!,
options:NSPropertyListMutabilityOptions.MutableContainersAndLeaves,
format: nil
)
if let dictionary = plistDic as? Dictionary< String, Dictionary<String, String> > {
print("\(dictionary)")
}
else {
print("Houston we have a problem")
}
}
catch let error as NSError {
print(error)
}
return defaultContent()
}
init(record:CKRecord) {
self.name = record.valueForKey(YALCityName) as! String
self.text = record.valueForKey(YALCityText) as! String
let imageData = record.valueForKey(YALCityPicture) as! NSData
self.image = UIImage(data:imageData)!
self.identifier = record.recordID.recordName
}
}
func ==(lhs: YALCity, rhs: YALCity) -> Bool {
return lhs.identifier == rhs.identifier
}
Try this code:
do {
var plistDic = try NSPropertyListSerialization.propertyListWithData(plistData!,
options:NSPropertyListMutabilityOptions.MutableContainersAndLeaves,
format: nil
)
// plistDic is of type 'AnyObject'. We need to cast it to the
// appropriate dictionary type before using it.
if let dictionary = plistDic as? Dictionary<String, Dictionary<String, String>> {
// You are good to go.
// Insert here your code that uses dictionary (otherwise
// the compiler will complain about unused variables).
// change 'let' for 'var' if you plan to modify the dictionary's
// contents.
// (...)
}
else {
// Cast to dictionary failed: plistDic is NOT a Dictionary with
// the structure: Dictionary<String, Dictionary<String, String>>
// It is either a dictionary of a different internal structure,
// or not a dictionary at all.
}
}
catch let error as NSError {
// Deserialization failed (see console for details:)
print(error)
}
Note: I split the call to a function that throws (try...) and the casting to your specific type of Dictionary (if let...) because I'm not really sure exactly what would happen if the call succeeds but the cast fails, or if it would be clear which one failed from the debugger. Also, I don't like too many things happening in one line...
EDIT: I fixed the options parameter. In Swift, Ints and enums aren't interchangeable; you need to pass the right type (I missed it the first time when modifying your code).