executeFetchRequest fatal error in Swift - ios

I'm new to Swift and I'm trying to learn while I write some Core Data related methods. I need to fetch some entities, this is a code snippet of the method I call for that:
let results = context.executeFetchRequest(fetchRequest, error: &error) as! [MyCustomEntity]?
if let myEntities = results {
let lastEntity = myEntities.last!
return lastEntity.entityNum.integerValue
}
When I run the app, it crashes at the line let lastEntity = myEntities.last! and I get this message in console:
fatal error: unexpectedly found nil while unwrapping an Optional value
However, error is nil at that point. I followed an example to write that code, and as far as I understood, the if statement block should only be executed if there are results... right? What is it happening there?
Thanks in advance

I'm assuming that the array that you're receiving is empty. You are using optional binding correctly to determine whether you receive an array or not, but that does not guarantee that you have elements in the array.
The last method on Array returns an Optional; it returns nil when the array is empty:
/// The last element, or `nil` if the array is empty
var last: T? { get }
You are unwrapping the return value from myEntities.last, which means your app crashes when myEntities is an empty array.
To make this code safe you'll additionally need to check the return value from the last method.

Related

calling function in viewDidLoad crash

Calling parseString crashes my application. myOptionalString is getting set from didSelectRowAtIndexPath in a tableview. The information is definitely get passed to this view controller.
The method also works if called from a button press. But in any life cycle method I try I get unexpectedly found nil while unwrapping an Optional.
override func viewDidLoad() {
super.viewDidLoad()
if let myUnwrappedString = myOptionalString{
print(myUnwrappedString) //<-- prints out string
confidence.text = parseString(myUnwrappedString) //<-- unexpectedly found nil while unwrapping an Optional crash
}
}
//this method works if called on a button press but crashes in viewDidLoad
func parseString(myString: String)->String{
return myString.substringFromIndex(myString.rangeOfString(" ")!.endIndex)
}
Your error come from your function parseString, let's see why. If you see carefully your function you're make a force-unwrapping of an optional value in your case the myString.rangeOfString(" ")!. This is not recommended at all.
If we pass the string "Confidence: 63%" to the function the function works properly and returns "63%", but it works because it have the " " string inside, if for some reason you pass some string that don't have it it will crash (e.g "Confidence:63%").
So one of the correct ways of implement this function can be using the guard statement using optional binding avoiding any force-unwrapping:
func parseString(myString: String) -> String? {
guard let range = myString.rangeOfString(" ") else { return nil }
return myString.substringFromIndex(range.endIndex)
}
In the above function you return nil in case of not exist the " " in the string and avoid the runtime error.
I hope this help you.
I'm guessing your problem lies in this line:
myString.rangeOfString(" ")!
rangeOfString returns an optional Range<Index>? value, meaning that if the character you are looking for is not present in the string, you will get the value nil in return. In other words, if there is no space in your string, you get a nil value for your range.
That value you then force unwrap with !, and then you use it to call substringFromIndex. This means that if the range was actually nil, you use that nil value to try to create a substring from its endIndex... which you cannot...so it crashes.
One way of solving this could be to use a guardto make sure that you actually found a range in your string. If you don't, then you just return an empty String...or do something else that suits your use case better.
Here is an example:
func parseString(myString: String)->String{
guard let range = myString.rangeOfString(" ") else {
return ""
}
return myString.substringFromIndex(range.endIndex)
}
Hope that helps you.

App crashed when getting string from Dictionary (swift)

I converted a JSON to Dictionary and got some String by
title = json?.objectForKey("Titel_Live") as! String
But some times app will be crashed. I cannot reproduce this problem, just get information from crash reports.
Could someone help me and tell why? Thanks
Error at line 163
Crash reports
title = json?.objectForKey(“Titel_live”) as! String
This line of code where you are doing force unwrapped (Don't force the cast using !) is the cause means if object with key Titel_live dot not find then should be crashed, better go with optional chaining or use gaurd but yes your Json does't contain any object with key Titel_live(may be spelling mistake or object is array so validate once).
//better go like this check if exist or not.
if let t = json?.objectForKey(“Titel_live”) {
title = t
}
You should not force the casting to String.
You can try :-
title = json?.objectForKey("Title_Live") as? String (if title is optional variable)
if title is not optional then use:
title = (json?.objectForKey("Title_Live") as? String ?? "")
Because objectForKey will return nil if no value is associated with that key and force casting nil to String fails and causes crash.

How to pass in a nil type into a paramter in Swift?

I am converting an iOS app from objective-c to swift. In the objective-c version of the app there was a method, AlertViewShow that gets called, and for one of its parameters nil was passed in like below.
AlertViewShow(APPNAME, INTERNET_NOT, nil);
The nil is passed in for a UIAlertViewDelegate type. Now when converting it to Swift I cannot do
AlertViewShow(APPNAME, INTERNET_NOT, nil) //error: cannot pass in nil into paramter
So, by looking at stack forums I tried this...
let alertViewNilObject: UIAlertViewDelegate? = nil
AlertViewShow(APPNAME, INTERNET_NOT, alertViewNilObject!) //compiler doesnt complain
So no errors pop up. But when I run the app it says: "fatal error: unwrapped a nil value".
How do I pass in nil into the parameter in Swift 2.0? Or is there another alternative besides passing in nil?
You need to make the argument itself take an optional value, rather than trying to force unwrap a nil optional (which will always crash).
func AlertViewShow(title:String, desc:String, delegate:AlertViewDelegate?) {
if delegate != nil {
// do some delegate-y things
}
...
}
...
AlertViewShow(title: APPNAME, desc: INTERNET_NOT, delegate: nil)
If it's an obj-c method that you're not re-writing in Swift, you'll just have to add the nullable prefix to the argument.
-(void) AlertViewShow:(NSString*)title desc:(NSString*)description delegate:(nullable UIAlertViewDelegate*)delegate;
(although it's worth noting that method names should start with a lowercase, not an uppercase)
That should then get converted to an optional argument when bridging.

unexpectedly found nil while unwrapping optional value, but there is some value

I am trying to get do a GET request, and save the following results from into NSMutuableArray. Then I am trying to load the following data I got into a tableview, but I am getting an "unexpectedly found nil while unwrapping optional value" whenever I try to configure the number of rows.
var dataArray:NSMutableArray?
Alamofire.request(.GET, "SOMERANDOMWEBSITE") .responseJSON { response in
// using if-let syntax to save json into our NSMutuableArray
if let JSON = response.result.value {
self.dataArray = JSON as? NSMutableArray
}
}
When i try to configure the number of rows:
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return self.dataArray.count
}
It gives me an error: "unexpectedly found nil while unwrapping optional value". But what's strange is that when I create a random button to see if the .count is indeed nil:
#IBAction func function(sender: AnyObject) {
print(self.dataArray?.count)
}
it returns me with Optional(2) instead of nil. Any ideas?
You made your array an Optional. We can see that here:
print(self.dataArray?.count)
So until it's populated in Alamofire's closure (the network request happens in the background), it is nil.
You quote:
return self.dataArray.count
but actually you probably have done
return self.dataArray!.count
This is why it crashes.
So you have to declare another option for count for when it's nil. A solution could be to safely unwrap the array then return the count or return a default value:
if let array = self.dataArray {
return array.count
}
return 0
This same logic can be expressed in a single line, as #brian-nickel notes in the comments:
return self.dataArray?.count ?? 0
Also, as stated by #vadian in the comments, absolutely, an Optional array makes little sense here, it would be better to just have a non-optional empty array from the beginning.
It's not strange, it's asynchronous.
Initially you declare dataArray as optional (aka nil) but don't initialize it.
The first attempt to update the table view fails (crashes) because calling count on nil is a bad idea.
Meanwhile the asynchronous request method returns the data and assigns it to dataArray which has now an (optional) value.
That's why you get a result in function.
Solution is to declare and initialize the array as non-optional Swift type.
var dataArray = [AnyObject]()
Probably there is a more specific type than AnyObject
and assign the returned data accordingly casting the result to the "better" type.
Basically declaring table view data source arrays as optional is absurd since your table view is always non-optional.

Unwrapping NSMutableArray from NSUserDefaults

I am trying to unwrap a NSMutableArray from user defaults, but keep getting the error unexpectedly found nil while unwrapping an Optional value. I tried to check for nil after getting the array, but it is happening on the line that is getting the array from the UserDefaults, so I don't know how to fix this error. Can anyone help?
var classURLs: NSMutableArray = NSMutableArray();
let defaults: NSUserDefaults = NSUserDefaults(suiteName: "group.myCompany.Array")!;
classURLs = NSMutableArray(object: defaults.objectForKey("Class URLs")!);
NSUserDefaults.objectForKey returns an optional for a reason – if the key isn’t present, you get nil back. You shouldn’t just force-unwrap it with !
The most common case is this is the first time you’ve tried reading it without having ever written it.
In which case you probably want a default value, perhaps an empty array:
let classURLs = defaults.stringArrayForKey("Class URLs") as? [String] ?? []
(?? substitutes the value on the right if the value on the left is nil)
Note, it’s probably better if you’re writing Swift to go with a Swift array (e.g. [String]) rather than NSMutableArray, unless all you are going to do is pass it straight into a call to Objective-C.
You can also avoid the ! you’re using with the NSUserDefaults init by using optional chaining:
let defaults = NSUserDefaults(suiteName: "group.myCompany.Array")
var classURLs = defaults?.stringArrayForKey("Class URLs") as? [String] ?? []
If the key does not exist, the forced unwrap (!) of the nil object will result in a crash. You need to account for this case.
var classURLs = NSMutableArray(object:
NSUserDefaults.standardUserDefaults().objectForKey("foo") ?? [])
NB: transitioning from Objective-C? You don't need the semicolons! ;-)

Resources