UIImage.init - imageLiteralResourceName crashing during App review

I have an error I don't seem to be able to resolve. I don't get this while testing on my phones, but it happens after uploading the App for review. The phone used in the received crash report is (iPhone XR). The crash report points to:

UIImage.init+ 92364 (imageLiteralResourceName:) + 144

This happens during app startup, and the code that using the UIImage function when it crash is:

let image = UIImage(imageLiteralResourceName: getCurrrentDayNumber())

... where the getCurrentDayNumber is my function, it returns the day number 1-31 depending on the day of the month as a string ("1" .."31"). This function using the Date() function, so it could be that the Date() function does not give a valid date during startup, and hence the imageLiteralResourceName gets wrong, but I thought that was unlikely.

Again, I don't get this error in any of the devices I have tested, nor in the Test build loaded to TestFlight, only during app review by Apple.

For the resources to load, I have created 31 image resources in Asset.xcassets. Each assets has three images, 1x,2x,3x for all 31 days in a month, where the 1x is typical 300x180 px.

Anyone that can point me to where I might find the issue?

Answered by OOPer in 686292022

So, I assume you use some code like this:

    func getCurrrentDayNumber() -> String {
        let currentDateTime = Date();
        let formatter = DateFormatter();
        formatter.timeStyle = .none;
        formatter.dateStyle = .long;
        let dateTimeString = formatter.string(from: currentDateTime);
        var dayNumber = dateTimeString.split(separator: " ");
        dayNumber = dayNumber[1].split(separator: ",");
        return String(dayNumber[0]);
    }

Frankly saying, this is a super bad implementation to get the day number (1...31).

I do not know which languages your app would support, but your getCurrrentDayNumber() returns August when region is set to United Kingdom even when Language is set to en. I believe that would cause the error you described.

Please try something like this:

    func getCurrrentDayNumber() -> String {
        let currentDateTime = Date()
        var gregorianCalendar = Calendar(identifier: .gregorian)
        gregorianCalendar.timeZone = TimeZone.current
        let dateComponents = gregorianCalendar.dateComponents([.day], from: currentDateTime)
        return String(dateComponents.day!)
    }

Generally, you should better not rely on the String generated by DateFormatter.Style.long.

Can you show the whole definition of getCurrentDayNumber?

Here is the getCurrentDayNumber:

func getCurrrentDayNumber() -> String {       let currentDateTime = Date();   let formatter = DateFormatter();   formatter.timeStyle = .none;   formatter.dateStyle = .long;       let dateTimeString = formatter.string(from: currentDateTime);   var dayNumber = dateTimeString.split(separator: " ");   dayNumber = dayNumber[1].split(separator: ",");        return = String(dayNumber[0]); }

Better show your code well-formatted. As the editing functionality is very limited in this site, you can use Your Answer to show some additional information. And = after return causes error...

Accepted Answer

So, I assume you use some code like this:

    func getCurrrentDayNumber() -> String {
        let currentDateTime = Date();
        let formatter = DateFormatter();
        formatter.timeStyle = .none;
        formatter.dateStyle = .long;
        let dateTimeString = formatter.string(from: currentDateTime);
        var dayNumber = dateTimeString.split(separator: " ");
        dayNumber = dayNumber[1].split(separator: ",");
        return String(dayNumber[0]);
    }

Frankly saying, this is a super bad implementation to get the day number (1...31).

I do not know which languages your app would support, but your getCurrrentDayNumber() returns August when region is set to United Kingdom even when Language is set to en. I believe that would cause the error you described.

Please try something like this:

    func getCurrrentDayNumber() -> String {
        let currentDateTime = Date()
        var gregorianCalendar = Calendar(identifier: .gregorian)
        gregorianCalendar.timeZone = TimeZone.current
        let dateComponents = gregorianCalendar.dateComponents([.day], from: currentDateTime)
        return String(dateComponents.day!)
    }

Generally, you should better not rely on the String generated by DateFormatter.Style.long.

Thanks a lot!

That makes a lot of sense, as the reviewer of the App might not be in the US, and I have only tested using the US language/location.

UIImage.init - imageLiteralResourceName crashing during App review
 
 
Q