Intel vs M1 Check

Is there anyone here that has an M1 and is familar with Xcode/Swift?
If so, could you execute this function and tell me what the returned results are?

On an Intel Mac it returns “x86_64”

private var getPlatform : String {
	var systeminfo = utsname()
	uname(&systeminfo)
	let machine = withUnsafeBytes(of: &systeminfo.machine) {bufPtr->String in
		let data = Data(bufPtr)
		if let lastIndex = data.lastIndex(where: {$0 != 0}) {
			return String(data: data[0...lastIndex], encoding: .isoLatin1)!
		} else {
			return String(data: data, encoding: .isoLatin1)!
		}
	}
	return machine
}

I think this has been asked & answered in
https://developer.apple.com/forums/thread/668206

However be wary - How Rosetta complicates call chains on M1 Macs – The Eclectic Light Company

The ‘uname’ utility is certainly useful for the result you’re after here.
But the ‘sysctl’ utility might also be useful for other and more detailed system information if needed.

For example these two “Terminal” give the same result.

uname -m

// And

sysctl -n hw.machine

But if you type this.

sysctl -a

You can get a full list of system information, which may not be what you’re after here, but might be useful for you at another time.

I can post a couple of Swift functions I created to use ‘sysctl’ in Swift projects if you’re interested.

Regards Mark

I really just need to know if the snippet I posted above returns “ARM64”

My translator needs to use the result when it calls Xcodebuild to insure it finds any errors that are platform related

I realise my reply was not specifically what you where asking here.
But just too offer an alternative to the ‘uname’ utility, just in case you might want other information, like say the processor type or name.

I haven’t touched Swift in a while, I wasn’t able to get it to run as is. This is what I tried. If you can adjust this so it runs, I can give it a go on an M1.

#!/usr/bin/env xcrun swift

private var getPlatform : String {
	var systeminfo = utsname()
	uname(&systeminfo)
	let machine = withUnsafeBytes(of: &systeminfo.machine) {bufPtr->String in
		let data = Data(bufPtr)
		if let lastIndex = data.lastIndex(where: {$0 != 0}) {
			return String(data: data[0...lastIndex], encoding: .isoLatin1)!
		} else {
			return String(data: data, encoding: .isoLatin1)!
		}
	}
	return machine
}

print getPlatform()

Thanks… but I’ve discovered the that code will sometimes Lie to you (Rosetta) and tell you its Intel when it is not… but sysctl is supposed to give you the true platfom

I created a public struct as an extension for the ‘sysctl’ feature of the “Foundation” framework.
which has two static functions, one for string return data types, and one for Int64 return data types.
because the ‘sysctl’ utility can return either of these two data types, depending on the requested key.

This is a modified version of the function I use for string return values.

import Foundation

func sysctlNameString(for key: String) -> String {
    if let keyString = key.cString(using: .utf8) {
        var size: Int = 0
        sysctlbyname(keyString, nil, &size, nil, 0)
        var name = [CChar](repeating: 0,  count: Int(size))
        sysctlbyname(keyString, &name, &size, nil, 0)
        return String(cString: name)
    } else {
        return "Error:"
    }
}

and you would call it like this.

let hardwareArchitecture = self.sysctlNameString(for: "hw.machine")

The function for the numeric data types is slightly different, but you don’t need it for the system type.
For a list of the available ‘sysctl’ key’s you can use this in the “Terminal”.

sysctl -a

Thanks I’ll check it out

:thinking: I’m pretty sure that’s me … :grin:

Considering I’ve never seen you help ANYONE (even if it is a real beginner’s question) it is not too surprising that you can’t help Dave who is Pro level … so maybe don’t try bragging with your incompetence? :grin:

4 Likes

No ! I’m Spartacus ! :stuck_out_tongue:

1 Like

Nice try, but:

Really, who would call himself “XojoPro”? Methinks someone is trying too hard? :roll_eyes:

Reminds me of “Life of Brian” and Longus Diccus … :grin:

Game, Set, and Match. Thanks for playing.

:smiling_imp:

I would guess most of the folks who frequent this forum could make that claim :stuck_out_tongue:

I believe it was Biggus Dickus.

Great film. :grinning_face_with_smiling_eyes:

1 Like

Indeed Biggus Dickus

3 Likes

Interesting fact (well, for me at least):

The letter K did not exist in the Latin alphabet originally, but was later borrowed from Greek, along with the letter Y, specifically to spell words that were borrowed from Greek. In other words K and Y were indicators of foreign words.

Usually the letter C was used for K and G sounds. My old latin teacher had a theory that I think is spot on:

The word Caesar developed into Kaiser in the West, and into Zar in the East.

Consequently Caesar would have originally be pronounced Ka-e-zar

I think we’re really drifting way off topic :stuck_out_tongue:

… but you are learning stuff …! :grin:

… and now back to your scheduled programming … :grin:

Lock the post! Ban some members!