MusicEventIteratorGetEventInfo in Xcode 8 beta 6 - swift

Up to Xcode 8 beta 5, I could access the event info in a MusicEventIterator, using something like:
var type: MusicEventType = 0
var stamp: MusicTimeStamp = 0
var data: UnsafePointer<()>? = nil
var size: UInt32 = 0
while(hasCurrentEvent != false) {
MusicEventIteratorGetEventInfo(iterator!, &stamp, &type,
&data, &size)
// do stuff with stamp, type, data...
}
I managed to get some help at the Apple dev site on dealing with "data", but getting data in the first place is also broken. I get a warning about using "UnsafeRawPointer" in the declaration of "data", and an error about not being able to use data as an inout argument in the call to MusicEventIteratorGetEventInfo()...
Though I know they're making things more deterministic and future-proof (i.e., for the compiler), it's incredibly frustrating to have the C-interop stuff changing every few months. My MIDI file parser code has already changed 3 times...
For reference, MusicEventIteratorGetEventInfo has the signature:
func MusicEventIteratorGetEventInfo(_ inIterator: MusicEventIterator,
_ outTimeStamp: UnsafeMutablePointer<MusicTimeStamp>,
_ outEventType: UnsafeMutablePointer<MusicEventType>,
_ outEventData: UnsafeMutablePointer<UnsafePointer<Void>>,
_ outEventDataSize: UnsafeMutablePointer<UInt32>) -> OSStatus
Any help greatly appreciated.

The latest reference of MusicEventIteratorGetEventInfo shows this:
Declaration
func MusicEventIteratorGetEventInfo(_ inIterator: MusicEventIterator,
_ outTimeStamp: UnsafeMutablePointer<MusicTimeStamp>,
_ outEventType: UnsafeMutablePointer<MusicEventType>,
_ outEventData: UnsafeMutablePointer<UnsafeRawPointer?>,
_ outEventDataSize: UnsafeMutablePointer<UInt32>) -> OSStatus
The third parameter outEventData is of type UnsafeMutablePointer<UnsafeRawPointer?>.
Generally, when an API claims UnsafeMutablePointer<T>, you declare a variable of type T, and pass it as an inout argument.
So, this should work:
var type: MusicEventType = 0
var stamp: MusicTimeStamp = 0
var data: UnsafeRawPointer? = nil //<- Declare a variable of type `UnsafeRawPointer?`.
var size: UInt32 = 0
while hasCurrentEvent != false {
MusicEventIteratorGetEventInfo(iterator!, &stamp, &type,
&data, &size) //<- Pass it as an inout argument.
// do stuff with stamp, type, data...
}

In case anyone else has the same problem, this seems to work:
// Somewhere to put the raw midi data
var type: MusicEventType = 0
var stamp: MusicTimeStamp = -1
let data: UnsafeMutablePointer<UnsafeRawPointer?> = UnsafeMutablePointer<UnsafeRawPointer?>.allocate(capacity: MemoryLayout<MusicEventType>.size)
var size: UInt32 = 0
var tsChangeStamp: MusicTimeStamp = 0
while(hasCurrentEvent != false) {
MusicEventIteratorGetEventInfo(iterator!, &stamp, &type, data, &size)
// do stuff...
}

Related

Cannot find 'strideofValue' in scope

I am getting:
Cannot find 'strideofValue' in scope
For below code:
public static func isDebuggerAttached() -> Bool {
var info = kinfo_proc()
var mib : [Int32] = [CTL_KERN, KERN_PROC, KERN_PROC_PID, getpid()]
var size = strideofValue(info)
let junk = sysctl(&mib, UInt32(mib.count), &info, &size, nil, 0)
assert(junk == 0, "sysctl failed")
return (info.kp_proc.p_flag & P_TRACED) != 0
}
How can I fix the logic?
I tried stride(ofValue:), but that just causes another strange issue:
Type of expression is ambiguous without more context
(With strange I mean that, it should say something like, function exists but not with ofValue label)
There is no strideofValue function in Swift 5, replace strideofValue(info) with MemoryLayout.stride(ofValue: info).

Unresolved identifier error in swift function with nested if loop

I'm currently teaching myself Swift coming from a background of Python recently and Visual Basic originally. I would describe myself as competent in those codes but a novice in Swift. I'm trying to write a function that will return a set number of digits from either the start or end of a long integer. My chosen method has been to convert the integer to a string and then use the prefix or suffix command.
Whilst I can get the function to work if it has no flow control and uses either prefix or suffix (first lot of code), when I try to write one function that does both I get an unresolved identifier error on the turnStringToInteger variable (second lot of code). I'm pretty sure this is because the variable lives within the if {} but if I declare it outside of the if loop (hashed out) this also errors. I appreciate this will have a really simple answer but how do I use the return correctly with a nested if loop?
This works...
//Function to Trim Integer (Prefix Only)
func integerTrim(integer:Int, trimLength:Int) -> Int {
var strFromInteger = String(integer)
var trimmedString = strFromInteger.prefix(trimLength)
var intFromString = Int(trimmedString) ?? 0
return intFromString
}
//Declare Input
var inputInt = 12345678910
//Call Function
var result = integerTrim(integer: inputInt, trimLength: 4)
//Print Results
print(inputInt)
print(result)
This doesn't...!
//Function to trim integer prefix or suffix
func integerTrim(integer:Int, type:String, trimLength:Int) -> Int {
var typeID = type
//var turnStringToInteger: Int
if typeID == "P" {
var turnIntegerToString = String(integer)
var trimmedString = turnIntegerToString.prefix(trimLength)
var turnStringToIngeger = Int(trimmedString) ?? 0
}
else if typeID == "S" {
var turnIntegerToString = String(integer)
var trimmedString = turnIntegerToString.suffix(trimLength)
var turnStringToIngeger = Int(trimmedString) ?? 0
}
return turnStringToInteger
}
//Declare Input
var inputInt = 53737363856453
//Call Function
var result = integerTrim(integer: inputInt, type: "P", trimLength: 4)
//Print Results
print(inputInt)
print(result)
As I am self taught I appreciate I may also not be using best practices. I really want to learn to do this properly so if I am going about all of this the wrong way to begin with I would be equally happy to hear other approaches. For example I did consider turning the integer to an array and then creating the trimmed integer from positions within this array. Would this be more elegant?
If you want to access the variable outside of the scope where it is assigned, you need to declare it in the outer scope.
If you do that without assigning it an initial value, you get an error: variable 'turnStringToInteger' used before being initialized. That happens because Swift sees a path in which turnStringToInteger never gets assigned a value (imagine what happens if "X" is passed in for type).
So your real issue is the use of String as the type for type. It would be better to use an enum that expresses exactly what you want:
enum TrimType {
case prefix, suffix
}
func integerTrim(integer: Int, type: TrimType, trimLength: Int) -> Int {
let typeID = type
var turnStringToInteger: Int
switch typeID {
case .prefix:
let turnIntegerToString = String(integer)
let trimmedString = turnIntegerToString.prefix(trimLength)
turnStringToInteger = Int(trimmedString) ?? 0
case .suffix:
let turnIntegerToString = String(integer)
let trimmedString = turnIntegerToString.suffix(trimLength)
turnStringToInteger = Int(trimmedString) ?? 0
}
return turnStringToInteger
}
Now there are only 2 possibilities for type and the switch handles both.
You call it like this:
let result = integerTrim(integer: inputInt, type: .prefix, trimLength: 4)
... after a little refactoring:
func integerTrim(integer: Int, type: TrimType, trimLength: Int) -> Int {
let turnIntegerToString = String(integer)
let trimmedString: Substring
switch type {
case .prefix:
trimmedString = turnIntegerToString.prefix(trimLength)
case .suffix:
trimmedString = turnIntegerToString.suffix(trimLength)
}
return Int(trimmedString) ?? 0
}
There's a few ways to do this. The root of the problem is your turnIntegerToString lifetime is within the braces - and the return is outside the braces.
func integerTrim(integer:Int, type:String, trimLength:Int) -> Int {
var typeID = type
var turnStringToInteger: Int = 0
// If you don't want to assign it to zero (your nil coalesce implies we can) - instead use...
// var turnStringToInteger: Int! // However - this can crash since your if statement does not cover all situations
if typeID == "P" {
var turnIntegerToString = String(integer)
var trimmedString = turnIntegerToString.prefix(trimLength)
turnStringToIngeger = Int(trimmedString) ?? 0
}
else if typeID == "S" {
var turnIntegerToString = String(integer)
var trimmedString = turnIntegerToString.suffix(trimLength)
turnStringToIngeger = Int(trimmedString) ?? 0
}
return turnStringToInteger
}

How to create a pointer in Swift?

I'm working with Swift 3.
I would like to have this C syntax :
int myVar;
int *pointer = &myVar;
So modifying pointer or myVar does the same exact same thing.
Also I don't know if it makes any difference, but in my case myVar is an array containing elements of a class and pointer is a pointer to one element of this array.
The & also exists in Swift but can only be used as part of a parameter list (e.g. init, func, closure).
var i = 5
let ptr = UnsafeMutablePointer(&i)
print(ptr.pointee) // 5
// or
let ptr = UnsafeMutablePointer<Int>.allocate(capacity: 1)
ptr.initialize(to: 5)
// or with a closure
let ptr: UnsafePointer = { $0 }(&i)
(Assuming I understand what you're asking for....)
Try the following code in a playground. It should print "99" three times.
class Row {
var rowNumber = 0
}
var rows = [Row]()
let testRow = Row()
testRow.rowNumber = 1
rows.append(testRow)
let selectedRow = rows[0]
selectedRow.rowNumber = 99
print(testRow.rowNumber)
print(selectedRow.rowNumber)
print(rows[0].rowNumber)
By default, there's no copying of objects as part of an assignment statement. If it were a struct, that would be different.
Adding a bit for completeness:
If you want a similar effect with scalar values instead of objects, Swift supplies various types of wrappers.
let intPointer = UnsafeMutablePointer<Int>.allocate(capacity: 8) // Should be 1, not 8 according to comment re: docs
let other = intPointer
other.pointee = 34
print(intPointer.pointee)
(Warning: I haven't used these wrappers for anything except experimenting in a playground. Don't trust it without doing some research.)
Same example as #Phillip. But I used struct. In this example rows[0] won't change:
struct Row {
var rowNumber = 0
}
var rows = [Row]()
var testRow = Row()
testRow.rowNumber = 1
rows.append(testRow)
var selectedRow = rows[0]
selectedRow.rowNumber = 99
print(testRow.rowNumber) // prints 1
print(selectedRow.rowNumber) // prints 99
print(rows[0].rowNumber) // prints 1
There are no C style pointers (Unsafe Pointer) as the question asks however objects are shared by reference and structures are by value:
Swift assign, pass and return a value by reference for reference type and by copy for Value Type
structures are always copied when they are passed around in your code, but classes are passed by reference.
For example
How to have pointers/ references to objects
class Song {
init(title: String, image: String, file: String, volume: Float, queuePlayer: AVQueuePlayer, playerLooper: AVPlayerLooper?) {
self.title = title
self.image = image
...
}
var title: String
var image: String
...
}
var aSong = Song(title: "", image: "", ...)
var arrOfSongReferences: [Song] = [Song]()
arrOfSongReferences.append(aSong)
var ptrToASong: Song = aSong
aSong = nil
// Due to Swift garbage collection ARC (Automatic Reference Counting), we still have references to the original aSong object so it won't be deleted
If data is struct you cannot do this
struct Song {
var title: String
var image: String
...
}
var aSong: Song = Song(title: "", image: "", ...)
var copyOfASong: Song = aSong
Method
You can also pass by reference into a function
// this would be inside a class, perhaps Player. It doesn't have to be a static btw
static func playSound(_ sound: inout Song, volume: Float = 0.0) {
if (sound.playerLooper == nil) {
...
}
}
// usage
Player.playSound(sound: &aSong)

How to pass an UnsafePointer<UInt8> to a C API by reference?

I wrote some Swift 2.2 code to interact with OpenSSL C APIs and now I'm trying to convert it to Swift 3.
In Swift 2
let octets = pkcs7_d_data(pkcs7_d_sign(receiptPKCS7).memory.contents)
var ptr = UnsafePointer<UInt8>(octets.memory.data)
// now pass pointer by reference
ASN1_get_object(&ptr, &length, &type, &xclass, end - ptr)
In Swift 3, I've had to make a couple changes
// use guard so i dont have to constantly unwrap these values
guard let octets = pkcs7_d_data(pkcs7_d_sign(receiptPKCS7).pointee.contents),
var ptr = UnsafePointer<UInt8>(octets.pointee.data) else {
return nil
}
ASN1_get_object(&ptr, &length, &type, &xclass, end - ptr)
// ^^^ this is now a compiler error
Unfortunately, I can no longer pass ptr to ASN1_get_object by reference, due to this error:
Cannot pass immutable value as inout argument: Implicit conversion from UnsafePointer<UInt8> to UnsafePointer<UInt8>? requires a temporary
and then the rest of the error is cut off (there's no expand arrow).
What I've tried:
changing the ptr assignment to UnsafePointer<UInt8>(octets.pointee.data)? but then I'm told that my existing assignment already produces an optional
changed the UnsafePointer to UnsafeMutablePointer
What needs to change here?
The problem seems to be that octets.pointee.data is a
UnsafeMutablePointer<UInt8>!, but ASN1_get_object expects
the address of a UnsafePointer<UInt8>?.
The following code compiles, but I could not test it:
if
let octets = pkcs7_d_data(pkcs7_d_sign(receiptPKCS7).pointee.contents),
let data = octets.pointee.data {
var ptr: UnsafePointer? = UnsafePointer(data) // (*)
var length = 0
var tag: Int32 = 0
var xclass: Int32 = 0
ASN1_get_object(&ptr, &length, &tag, &xclass, Int(octets.pointee.length))
}
(*) is the pointer conversion which makes it compile.

Convert a Swift Array of String to a to a C string array pointer

I'm on Swift 3, and I need to interact with an C API, which accepts a NULL-terminated list of strings, for example
const char *cmd[] = {"name1", "value1", NULL};
command(cmd);
In Swift, the API was imported like
func command(_ args: UnsafeMutablePointer<UnsafePointer<Int8>?>!)
After trying hundreds of times using type casting or unsafeAddress(of:) I still cannot get this work. Even though I pass a valid pointer that passed compilation, it crashes at runtime saying invalid memory access (at strlen function). Or maybe it's something about ARC?
let array = ["name1", "value1", nil]
// ???
// args: UnsafeMutablePointer<UnsafePointer<Int8>?>
command(args)
You can proceed similarly as in How to pass an array of Swift strings to a C function taking a char ** parameter. It is a bit different because of the different
const-ness of the argument array, and because there is a terminating
nil (which must not be passed to strdup()).
This is how it should work:
let array: [String?] = ["name1", "name2", nil]
// Create [UnsafePointer<Int8>]:
var cargs = array.map { $0.flatMap { UnsafePointer<Int8>(strdup($0)) } }
// Call C function:
let result = command(&cargs)
// Free the duplicated strings:
for ptr in cargs { free(UnsafeMutablePointer(mutating: ptr)) }
This class provides a pointer that works with char** and automatically deallocates the memory, even though it's a struct (using a little trick with a mapped data with deallocator).
public struct CStringArray {
public let pointer: UnsafeMutablePointer<UnsafeMutablePointer<CChar>?>
public let count: Int
private var data: Data
public init(_ array: [String]) {
let count = array.count
// Allocate memory to hold the CStrings and a terminating nil
let pointer = UnsafeMutablePointer<UnsafeMutablePointer<CChar>?>.allocate(capacity: count + 1)
pointer.initialize(repeating: nil, count: count + 1) // Implicit terminating nil at the end of the array
// Populate the allocated memory with pointers to CStrings
var e = 0
array.forEach {
pointer[e] = strdup($0)
e += 1
}
// This uses the deallocator available on the data structure as a solution to the fact that structs do not have `deinit`
self.data = Data(bytesNoCopy: pointer, count: MemoryLayout<UnsafeMutablePointer<CChar>>.size * count, deallocator: .custom({_,_ in
for i in 0...count - 1 {
free(pointer[i])
}
pointer.deallocate()
}))
self.pointer = pointer
self.count = array.count
}
public subscript(index: Data.Index) -> UnsafeMutablePointer<CChar>? {
get {
precondition(index >= 0 && index < count, "Index out of range")
return pointer[index]
}
}
public subscript(index: Data.Index) -> String? {
get {
precondition(index >= 0 && index < count, "Index out of range")
if let pointee = pointer[index] {
return String(cString: pointee)
}
return nil
}
}
}