You've got different possibilities. First you can add a return-value to your function and return your string-array.
func yourFunction()->[String]{
var headlinesString:String = Headlines["text"] as String
var headlinesArray:[String] = headlinesString.componentsSeparatedByString("\n")
return headlinesArray
}
var headLinesLabels = yourFunction()
Or you can just set the value of your headLinesLabels
in the function itself:
func yourFunction(){
var headlinesString:String = Headlines["text"] as String
var headlinesArray:[String] = headlinesString.componentsSeparatedByString("\n")
headLinesLabels = headLinesArray
}
That way you don't need to return a value but you set it right in your function. But I would recommend to use the first possibility.
As you maybe noticed, I've edited your existing code a bit. First you should use String
instead of NSString
if you are using Swift.
Also variable-names should always be camelCase. So change HeadlinesString
to headlinesString
.
Also you don't need to cast your objects you receive from the componentsSeparatedByString
method into an NSArray, but you can use [String]
to cast it to an array with String
elements. The same with the headLinesLabels
variable. Just initialize it like that:
var headlinesLabels:[String]!
That way, Swift knows that this array should contain String
elements and you can set it at anytime later like that:
headlinesLabels = /*yourStringArray*/
Array
instead ofNSArray
this would be a lot less of a concern - Airspeed Velocity