0

I'm working on a project in swift 3.0 where I have a tuple array to store set of data. Once I save them I convert this tuple array to a NSArray and I shuffle the elements (objects) in that array using another function. My requirement is to access elements in this retuned array (NSArray). My code as bellow(Please read the comments for better understand).

This is where I save data to my tuple array

 var savedSongsArray:[(title:String,healerName:String,trackUrl:URL,imageUrl: String)] = []

func getAllTracksFromSavedPath(){
        for i in 0 ..< trackIdsArray.count{

             savedSongsArray.append((title: songDetailsArray[i].title , healerName:songDetailsArray[i].healerName, trackUrl: uRL, imageUrl: songDetailsArray[i].imageUrl))
            //savedSongsArray is my tuple array
        }

    let arrayToBeShuffled = savedSongsArray as NSArray // This is where I convert it to a NSArray
}

My shuffle function

    func shuffledSongsArray(array:NSArray) -> NSArray {
    let mutableArray = array.mutableCopy() as! NSMutableArray
    let count = mutableArray.count
    if count>1 {
        for i in ((0 + 1)...count-1).reversed(){
            mutableArray.exchangeObject(at: i, withObjectAt: Int(arc4random_uniform(UInt32(i+1))))
        }
    }

    return mutableArray as NSArray
}

Once I print the result of the mutableArray its as bellow

("indika - (index.jpeg)", "Steven Flemin", file:///Users/auxenta/Library/Developer/CoreSimulator/Devices/2B4EB1F8-FA38-4777-A274-10979F6EEB6F/data/Containers/Data/Application/CAB78797-8AB2-4DD9-8322-6F88B454BA74/Documents/5746821397741568.mp3, "https://storage.googleapis.com/feisty-beacon-159305.appspot.com/images.png")

how can I access individual elements in this tuple NSArray??

Hamish
  • 78,605
  • 19
  • 187
  • 280
danu
  • 1,079
  • 5
  • 16
  • 48
  • can you explain your requirement because can't understand your question . – KKRocks May 31 '17 at 08:44
  • If u can see the bottom of my code I've pasted the result I get once I shuffle the array. My requirement is to access each of these elements and assign to properties as I've mentioned underneath – danu May 31 '17 at 08:50
  • Why are you using `NSArray` here? You're throwing away type information. Your shuffling function would be better placed in an extension of `MutableCollection`, compare [How do I shuffle an array in Swift?](https://stackoverflow.com/q/24026510/2976878). Also you probably want to use an actual `struct` rather than tuples in your array. Tuples are only really meant to be used for short-term data storage. – Hamish May 31 '17 at 09:19
  • Im sorrry Im new to swift, canu be more specific with a code snippet – danu May 31 '17 at 09:25

1 Answers1

0

First of all, use native Swift structs, namely Array instead of NSArray and NSMutableArray. You can access elements of a named tuple like this: let title = array[n].title, where n is the index of the tuple you want to access and title is the named element of the tuple you want to get.

This should work:

func shuffledSongsArray(array:[(title:String,healerName:String,trackUrl:URL,imageUrl: String)]) -> [(title:String,healerName:String,trackUrl:URL,imageUrl: String)] {
    let shuffledArray: [(title:String,healerName:String,trackUrl:URL,imageUrl: String)] = []
    //do the shuffling here
    return shuffledArray
}

let result = shuffledSongsArray(array: inputArray)
let firstHealerName = result[0].healerName

or let firstHealerName = result[0].1 should give you the same result

Dávid Pásztor
  • 51,403
  • 9
  • 85
  • 116
  • If this didn't answer your question, please explain in more detail what do you want to achieve. – Dávid Pásztor May 31 '17 at 08:54
  • This is the exact thing what I desire to achieve. But the fact from my shuffled function it returns an NSArray. the result i have printed.Now for instance from that result how could i get the value "Steven Flemin" out and assign it to a value? – danu May 31 '17 at 09:01
  • I have added the example for your code. You should be able to figure out how to convert your code to use `Array` instead of `NSArray` – Dávid Pásztor May 31 '17 at 09:09
  • i says result has no member called healerName. I think the issue is shuffledSongsArray returns a NSArray as in my code in the question. how could i fix this – danu May 31 '17 at 09:15
  • Then try the second option I have given. If you were using Array as I have advised instead of NSArray and returning a named tuple, the named parameter would work as well. – Dávid Pásztor May 31 '17 at 09:17
  • @ Dávid Pásztor, I really do need to solve this issue and I think u get my question. But since I'm new to swift I clearly not getting what u trying to say. Where exactly should i change in my code??. non of the methods working above – danu May 31 '17 at 09:24
  • Can u help to change my "shuffledSongsArray" method – danu May 31 '17 at 09:33
  • I have added a function skeleton so you can see what types you should be returning. For the array shuffling, you can find several methods from other questions here on stack overflow. – Dávid Pásztor May 31 '17 at 09:35
  • the issue with your proposed method is that i cannot write my shuffle logic inside that. Specially I cannot call the member "exchangeObject" inside my shuffle function. – danu May 31 '17 at 09:44
  • As I have told you, the question of shuffling an Array in Swift has been answered on the site several times, for example in [this question](https://stackoverflow.com/q/24026510/2976878), which has already linked to you by Hamish – Dávid Pásztor May 31 '17 at 09:47