Author Topic: Reading Tags on iOS (Swift)  (Read 176 times)

bbarlocco

  • Posts: 2
Reading Tags on iOS (Swift)
« on: 22 Sep '17 - 08:59 »
Hi, im working on Revibro project and having trouble reading OGG tags on Swift 3.
The problem is that i recieve an UnsafePointer, and I have to transform it into an Array of Strings, without knowing how much memory is using. Can you tell me how to proceed??
 Thanks!!

Ian @ un4seen

  • Administrator
  • Posts: 20389
Re: Reading Tags on iOS (Swift)
« Reply #1 on: 22 Sep '17 - 13:19 »
I'm not a Swift user myself, so I'm not certain, but I think you would do something like this:

Code: [Select]
const char *tags = BASS_ChannelGetTags(handle, BASS_TAG_OGG); // get OGG tags
if (tags) {
while (*tags) {
tagsarray.append(String.fromCString(tags)); // convert tag and add to array
tags += strlen(tags) + 1; // move on to next tag
}
}

bbarlocco

  • Posts: 2
Re: Reading Tags on iOS (Swift)
« Reply #2 on: 25 Sep '17 - 07:41 »
Hello Ian, thanks for the quick reply, i find your code not suittable for using it on Swift, but ive found the solution myself, a little bit ugly but it works fine
Code: [Select]
:

        var tags:UnsafePointer<Int8> = BASS_ChannelGetTags(channel, DWORD(BASS_TAG_OGG))
       
       
            var metadata:String = ""
            var lastCharacterIs0:Bool = false
            var isFinished:Bool = false
            var completeMetadata:[String] = []
            while (!isFinished) {
                let point:Int8 = tags.pointee
                let caracter:String = String(format: "%c", point)
                //I finish when finding two consecutive nulls
                if (point == 0 && !lastCharacterIs0) {
                    lastCharacterIs0 = true
                } else if (point == 0 && !isFinished) {
                    isFinished = true
                    completeMetadata.append(metadata)
                } else {
                    if lastCharacterIs0 {
                        lastCharacterIs0 = false
                        completeMetadata.append(metadata)
                        metadata = ""
                    }
                    metadata.append(caracter)
                }
                tags = tags + 1
            }
« Last Edit: 25 Sep '17 - 07:48 by bbarlocco »