Reputation: 681
In this example, I am creating a new empty array of Int
and I want to add certain characters from the other array into it. Originally, in my characters
array I store numbers ["1","","2","","3"]
and in this array (I might be doing this completely wrong) I want to iterate through those numbers to produce a newarray
which will have these numbers [1,2,3]
(essentially removing the spaces in between and making them of type Int
)
As far as I understand, I have created a dynamic array i.e. no specific size, but I am getting this error : fatal error: Array index out of range
and I am not sure why. Could someone clarify this for me? Thanks
do{
var data = try String(contentsOfFile: documentsDirectoryPath as String,
encoding: NSASCIIStringEncoding)
print(data)
let characters = Array(data.characters)
print(characters)
var newarray = [Int]()
for var index = 0; index <= characters.count ; ++index {
if characters[index] == " " {
index++
}
else{
newarray[index] = index
}
}
print(newarray)
}
catch{
print("error")
}
}
Upvotes: 0
Views: 2223
Reputation: 1170
Seeing from your other comments in this thread it appears you are trying to filter out spaces and commas from a string that may look like "1, 2, 3"
. Correct me if this is wrong. The completely swifty solution to this is as follows:
do {
var data = try String(contentsOfFile: documentsDirectoryPath as String, encoding: NSASCIIStringEncoding)
print(data)
let newarray = data.characters.filter { Int(String($0)) != nil }.map { Int(String($0))! }
} catch {
print("error")
}
filter
will return an array of Character
s that convert to integers. map
will then take that array and then transform each element into an integer. We have to convert the Character
to a String
type because there is no Int
constructor that takes a Character
.
Upvotes: 2
Reputation: 4393
change this line:
for var index = 0; index <= characters.count ; ++index {
to this one:
for var index = 0; index < characters.count ; ++index {
Upvotes: 0
Reputation: 5741
Another approach:
First remove all spaces from the String
, then convert it to Array
:
let trimmedData = data.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceCharacterSet())
let characters = Array(trimmedData.characters)
Edit: if you just want to keep the numbers:
let validNumbers = "1234567890"
let validSet = NSCharacterSet(charactersInString: validNumbers)
let trimmedData = data.stringByTrimmingCharactersInSet(validSet)
let characters = Array(trimmedData.characters)
Upvotes: 0
Reputation: 727047
You have several issues in your code, most important of which is that your approach is incorrect, because it treats a string as a collection of individual characters, while your problem calls for parsing multi-digit numbers.
A better approach is to split the array on commas, and parse integers from trimmed results, like this:
let data = "12, 23, 345"
print(data)
let newarray = data.characters.split(){$0 == ","}.map{
Int(String.init($0).stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceCharacterSet()))!
}
print(newarray) // 12, 23, 345
Upvotes: 1
Reputation: 4654
Based on what you told in comments (you want to remove space chars from array and get the new one), you can achieve it like so:
let newarray = characters.filter { $0 != " " }
Upvotes: 0
Reputation: 4654
This happens because your newarray
has no capacity.
Try using append
method or create array with capacity like so:
let newarray = [Int](count:charactes.count, repeatedValue: 0)
.
P.S. Also note that you have mistake in for loop, you should use <
instead of <=
and hustling with iterator value within the loop is a bad idea, you increment this var twice when you see a space, are you sure that is intended?
And you are using two bad practices in Swift. First of all, you use C-like for-loop which will be removed in Swift 3. Same goes for ++
operators. They will be removed too.
Use for-in
and +=
instead. This will make your code more Swift-ish.
Upvotes: 0