dbus
dbus

Reputation: 73

How can I look up the local computer name in swift?

I am trying to return a string of the local computer name (as shown in sharing in system preferences).

I have found this method in objective-c but i'm unsure how I can convert this to swift and set a string variable to the returned value.

[[NSHost currentHost] localizedName];

If anyone could help that would be great!

Thanks :)

Upvotes: 4

Views: 4822

Answers (1)

Leo Dabus
Leo Dabus

Reputation: 236568

edit/update

Xcode 8 beta • Swift 3

let currentHost = Host.current().localizedName ?? ""


NSHost localizedName property returns an optional String. You can use if let or "??" nil coalescing operator to unwrap the String returned:

let currentHost = NSHost.currentHost().localizedName ?? ""

Upvotes: 6

Related Questions