fatal error: Index out of range (swift) - ios

Below is my code in the editor for hackerrank. Im getting a fatoal error index out of range in the language swift. I have similar code on my playground, but instead of taking a readLine im just getting a random number to fill my 2D array. This is working on my playground
//hackerrank
https://ghostbin.com/paste/jtc5k
//xcode where it works
https://ghostbin.com/paste/xqqh4

You can't use ReadLine() in playground file.
See this
So your line: if let input = readLine(),value = Int(input) doesn't execute therefore your second dimension of array doesn't get fill.
You can change your code to be look like this to see the difference:
if let input = readLine(),value = Int(input)
{
array[row].append(value)
} else {
array[row].append(Int(arc4random_uniform(2)))
}

Related

How can I find the last coordinate in a MGLPolyline?

I have some code which is supposed to find the first and final coordinate in an MGLPolyline. Currently, I am able to find the first and the middle value, but am not able to find the last value. Bellow is my code. In it, you can see how I find the first and middle coordinate.
var arrayOfCoordinates = [CLLocationCoordinate2D]()
var arrayOfPolylines = [MGLPolyline]()
for value in arrayOfPolylines {
mapView.addAnnotation(value)
//This is for the first and last snakehead and tail stuff
let lastLoc = value.coordinate.latitude
let newLoc = value.coordinate.longitude
let firstCor = value.coordinates[0].latitude
let secondCor = value.coordinates[0].longitude
...
I have tried the following to find the value:
value.coordinates[value.coordinates.count] //This does not compile as it says
Value of type 'UnsafeMutablePointer' has no member 'count'
So my question is how can I find the last value (coordinate)?
Well, your code will not work for this. With the current code, you would not be able to find the last value easily, you would have to do a mathematical process to determine the count of coordinates then check if the for loop has run through enough time to reach the last coordinate.
But an easier way is to create instead an array of CLLocation which then you can access with the following code:
let lastLoc = self.arrayOfLocationList.last //last value here
let newLoc = self.arrayOfLocationList.first

App crashes if playlist count = 0 (Do empty playlists have a persistentid?)

My app is crashing if a playlist is empty (no songs). My app works for all non-empty playlists. It seems like there isn't a persistentid for an empty playlist, but I think I am wrong on that.
let qryPlaylists = MPMediaQuery.playlistsQuery()
var selectedPlaylistTitle: String!
var selectedPlaylistPersistentID: UInt64!
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let indexPath: NSIndexPath = playlistsTableView.indexPathForSelectedRow!
let rowItem = qryPlaylists.collections![indexPath.row]
let playlistSize = rowItem.count
print("playlistSize = ", playlistSize)
selectedPlaylistTitle = rowItem.valueForProperty(MPMediaPlaylistPropertyName) as! String
print("selectedPlaylistTitle = ", selectedPlaylistTitle)
// Will crash on this next line if the playlistSize = 0
selectedPlaylistPersistentID = rowItem.items[0].persistentID
// If I use following line instead, it will crash on any playlist
// selectedPlaylistPersistentID = rowItem.valueForProperty(MPMediaPlaylistPropertyPersistentID) as! UInt64
// This line will never be printed
print("selectedPlaylistPersistentID = ", selectedPlaylistPersistentID)
}
Thanks in advance for any help!
If an array such as items is empty, it has no index 0 and will crash if you refer to it, with an out-of-bounds error. So if you don't want to crash, don't do that. You already know that items is empty, because rowItem.count told you so; as you said yourself, playlistSize is 0 in that case.
A simple-minded way to look at it is this: the largest legal index in an array is one less than its count.
Another issue you asked about is that this line always crashes:
selectedPlaylistPersistentID = rowItem.valueForProperty(MPMediaPlaylistPropertyPersistentID) as! UInt64
The problem here is that you are apparently using Swift 2.x. (You should have said that in your question; I deduce it, though, from the fact that valueForProperty has not changed to value(forProperty:), which is what it is called in Swift 3.)
In Swift 2, you cannot cast directly down to UInt64. (I am surprised that the compiler does not draw your attention to this fact with a warning.) Thus, the cast fails and you crash. You need to cast down to NSNumber and then take that NSNumber's unsignedLongLongValue.
And while you are doing this, you really should stop using exclamation marks in your code. When I say "cast down", I mean "cast down safely". My own Swift 2 code for doing this sort of thing looks like this:
if let id = (rowItem.valueForProperty(MPMediaItemPropertyAlbumPersistentID) as? NSNumber)?.unsignedLongLongValue {
// ...
}

Expected Sequence expression swift 3

I'm using a loop to set the original image for tab bar items so the unselected state isn't gray (my original icons are white). However, it looks like the recent Xcode 8 update broke the code:
for (items in 0 ..< tabBar.items!.count ){
let tabItemIndex = tabBar.items![items]
tabItemIndex.image = tabItemIndex.image!.withRenderingMode(UIImageRenderingMode.alwaysOriginal)
}
}
I'm getting the following errors on the first line: Expected 'in' after for-each pattern, Expected Sequence expression for for-each loop, and Expected pattern.
Can anyone please help me fix this solution? It worked great until today.
Thanks!!
for x in y is an actual expression in Swift. You cannot break it up with parentheses, for (x in y) — that separates the for from the in and causes the expression to seem like nonsense to the compiler.
So, delete the parentheses and all will be well.
You have some issues on how you're creating your loop, and some very unsafe forced unwrapping. Try this:
if let items = tabBar.items {
for tabBarItem in items {
if let image = tabBarItem.image {
tabBarItem.image = image.withRenderingMode(UIImageRenderingMode.alwaysOriginal)
}
}
}
Or even cleaner, like this:
tabBar.items?.forEach { tabBarItem in
if let image = tabBarItem.image {
tabBarItem.image = image.withRenderingMode(UIImageRenderingMode.alwaysOriginal)
}
}
In Swift, unlike in Objective-C, there are no parentheses involved in this structure. Remove the parentheses to fix your syntax error.
However, there are style and safety issues here other than the syntax error: see picciano's answer for a much cleaner and safer way to rewrite the loop.

Cast from 'MDLMaterialProperty?!'?

I'm working with the parse framework for an App that I'm converting to Xcode 7. One of the interesting errors which has occurred as part of migrating the project is the following:
Cast from 'MDLMaterialProperty?!' to unrelated type 'PFUser' always fails
The offending line seems to be the "if let" clause below. I've commented the old line which worked fine in the previous version of Swift for comparison.
With respect to what this is actually doing - I've passed an array of Parse objects into "likesForThankful" where a pointer "userID" refers to a related PFUser. As part of this method I'm writing individual PFUsers to an array.
Any help is appreciated - thanks in advance.
//Add PFUsers who Like Post to our FeedItem
private func callbackFromLikesProcessing(likesForThankful:[AnyObject], againstFeedItem:FeedItem){
//Instantiate our Objective C compatible array for processing later
againstFeedItem.parseUsersObjectsWhoLikePost = NSMutableArray()
//Loop through likes and add PFUsers to array of users who like post
for usersWhoLikePost in likesForThankful{
// if let parseUserWhoLikesPost = usersWhoLikePost["userID"] as PFUser{
if let parseUserWhoLikesPost = usersWhoLikePost["userID"] as? PFUser {
againstFeedItem.parseUsersObjectsWhoLikePost.addObject(parseUserWhoLikesPost)
}
}
Andrew
Figured this out if it can help anyone - it's basic Swift Syntax (though I'm not sure why the compiler let me get away with this in the first version of Swift!).
Because usersWhoLikePost is a PFObject which happens to contain a pointer to a PFUser object I needed to conditionally unwrap this first like so:
for usersWhoLikePost in likesForThankful{
if let parseLikeObject = usersWhoLikePost as? PFObject{
if let parseUserWhoLikesPost = parseLikeObject["userID"] as? PFUser {
againstFeedItem.parseUsersObjectsWhoLikePost.addObject(parseUserWhoLikesPost)
}
}
}
After this I could continue as I had previously done and access the "userID" property with a conditional unwrap of the PFUser object.

How to list (almost) all emojis in Swift for iOS 8 without using any form of lookup tables?

I'm playing around with emojis in Swift using Xcode playground for some simple iOS8 apps. For this, I want to create something similar to a unicode/emoji map/description.
In order to do this, I need to have a loop that would allow me to print out a list of emojis. I was thinking of something along these lines
for i in 0x1F601 - 0x1F64F {
var hex = String(format:"%2X", i)
println("\u{\(hex)}") //Is there another way to create UTF8 string corresponding to emoji
}
But the println() throws an error
Expected '}'in \u{...} escape sequence.
Is there a simple way to do this which I am missing?
I understand that not all entries will correspond to an emoji. Also, I'm able create a lookup table with reference from http://apps.timwhitlock.info/emoji/tables/unicode, but I would like a lazy/easy method of achieving the same.
You can loop over those hex values with a Range: 0x1F601...0x1F64F and then create the Strings using a UnicodeScalar:
for i in 0x1F601...0x1F64F {
guard let scalar = UnicodeScalar(i) else { continue }
let c = String(scalar)
print(c)
}
Outputs:
😁😂😃😄😅😆😇😈😉😊😋😌😍😎😏😐😑😒😓😔😕😖😗😘😙😚😛😜😝😞😟😠😡😢😣😤😥😦😧😨😩😪😫😬😭😮😯😰😱😲😳😴😵😶😷😸😹😺😻😼😽😾😿🙀🙁🙂🙃🙄🙅🙆🙇🙈🙉🙊🙋🙌🙍🙎🙏
If you want all the emoji, just add another loop over an array of ranges:
// NOTE: These ranges are still just a subset of all the emoji characters;
// they seem to be all over the place...
let emojiRanges = [
0x1F601...0x1F64F,
0x2702...0x27B0,
0x1F680...0x1F6C0,
0x1F170...0x1F251
]
for range in emojiRanges {
for i in range {
guard let scalar = UnicodeScalar(i) else { continue }
let c = String(scalar)
print(c)
}
}
For those asking, the full list of available emojis can be found here: https://www.unicode.org/emoji/charts/full-emoji-list.html
A parsable list of unicode sequences for all emojis can be found in the emoji-sequences.txt file under the directory for the version you're interested in here: http://unicode.org/Public/emoji/
As of 9/15/2021 the latest version of the emoji standard available on Apple devices is 13.1.

Resources