0

This is my string:

(
    "2|Apples / amount",
    "5|Pizza / pieces",
    "10|Oranges / amount",
    "1|Brownie / piece"
)

How to get the number, name of the food and unit of the food in arrays

var number:NSMutableArray = NSMutableArray()
var foodName:NSMutableArray = NSMutableArray()
var foodUnit:NSMutableArray = NSMutableArray()
Bogdan Bogdanov
  • 882
  • 11
  • 36
  • 79
  • 2
    That's not a string, it's an array with 4 strings in it. – rdelmar Oct 29 '14 at 23:55
  • it was array but I inserted in string now I want to make an arrays from this string, the code is the println of the string – Bogdan Bogdanov Oct 29 '14 at 23:56
  • Your question is not clear. You say you have a string, but what you show is an array. Log your string and post the exact log results so we can see what your string actually looks like. – rdelmar Oct 30 '14 at 00:17
  • this is the exact log result of the string via println() I got it from Core Data in String before I added it in Core Data it was in array but when you add array in Core Data it turns into a String – Bogdan Bogdanov Oct 30 '14 at 00:20

1 Answers1

1

You will need to Split a string into an array.

Not tested but this is the gist of it:

    let myFood = ["2|Apples / amount", "5|Pizza / pieces", "10|Oranges / amount", "1|Brownie / piece"]

foreach value in myFood {

let numberAndInfo = myFood.componentsSeparatedByString("|")
// Insert numberAndInfo[0] in your number array.
let foodAndAmount = numberAndInfo[1].componentsSeparatedByString(" / ")
// Insert foodAndAmount[0] into foodName
// Insert foodAndAmount[1] into foodUnit

}

EDIT: Apologize for the indentation.

Community
  • 1
  • 1
Andy Ibanez
  • 12,104
  • 9
  • 65
  • 100
  • yes but I don't have an array, the code with food amount and units is string, I know how to do it with array but it's not an array... – Bogdan Bogdanov Oct 30 '14 at 00:05
  • I don't think I get it. So you only have one string like this?: "2|Apples / amount5|Pizza / pieces10|Oranges / amount1|Brownie / piece" - Your initial code does show an array of strings. – Andy Ibanez Oct 30 '14 at 00:11
  • I had an array but in Core Data you have to added in to String and now when I get it from Core Data is String not an array and now I want to get the info from this string in arrays again – Bogdan Bogdanov Oct 30 '14 at 00:14