Mac Developer Library

Developer

Foundation Framework Reference NSHost Class Reference

Options
Deployment Target:

On This Page
Language:

NSHost

Inheritance


Conforms To


Import Statement


Swift

import Foundation

Objective-C

@import Foundation;

Availability


Available in OS X v10.0 and later.

The NSHost class provides methods to access the network name and address information for a host. Instances of the NSHost class represent individual hosts on a network. Use NSHost objects to get the current host’s name and address and to look up other hosts by name or by address.

To create an NSHost object, use the currentHost, hostWithAddress:, or hostWithName: class methods (don’t use alloc and init). These methods use available network administration services (such as NetInfo or the Domain Name Service) to discover all names and addresses for the host requested. They don’t attempt to contact the host itself, however. This approach avoids untimely delays due to a host being unavailable, but it may result in incomplete information about the host.

An NSHost object contains all of the network addresses and names discovered for a given host by the network administration services. Each NSHost object typically contains one unique address, but it may have more than one name. If an NSHost object has more than one name, the additional names are variations on the same name, typically the basic host name plus the fully qualified domain name. For example, with a host name “sales” in the domain “anycorp.com”, an NSHost object can hold both the names “sales” and “sales.anycorp.com”.

NSHost methods are thread-safe.

  • Returns an NSHost object representing the host the process is running on.

    Declaration

    Swift

    class func currentHost() -> Self

    Objective-C

    + (instancetype)currentHost

    Return Value

    NSHost object for the process’s host.

    Import Statement

    Objective-C

    @import Foundation;

    Swift

    import Foundation

    Availability

    Available in OS X v10.0 and later.

  • Returns the NSHost with the Internet address address.

    Declaration

    Swift

    convenience init(address address: String)

    Objective-C

    + (instancetype)hostWithAddress:(NSString *)address

    Parameters

    address

    Network address to look up. For example, @"127.0.0.1" or @"fe80::1".

    Return Value

    The host for address.

    Import Statement

    Objective-C

    @import Foundation;

    Swift

    import Foundation

    Availability

    Available in OS X v10.0 and later.

    See Also

    + hostWithName:

  • Returns a host with a specific name.

    Declaration

    Swift

    convenience init(name hostname: String?)

    Objective-C

    + (instancetype)hostWithName:(NSString *)hostname

    Parameters

    hostname

    Name of the host to look up. Can be either a simple hostname, such as @"sales", or a fully qualified domain name, such as @"sales.anycorp.com".

    Return Value

    The host named hostname.

    Import Statement

    Objective-C

    @import Foundation;

    Swift

    import Foundation

    Availability

    Available in OS X v10.0 and later.

  • Returns one of the network addresses of the receiver.

    Declaration

    Swift

    var address: String? { get }

    Objective-C

    @property(readonly, copy) NSString *address

    Return Value

    One of the network address for the receiver. For example, @"192.42.172.1" or @"fe80::1".

    Import Statement

    Objective-C

    @import Foundation;

    Swift

    import Foundation

    Availability

    Available in OS X v10.0 and later.

  • Returns all the network addresses of the receiver.

    Declaration

    Swift

    var addresses: [AnyObject]! { get }

    Objective-C

    @property(readonly, copy) NSArray *addresses

    Return Value

    All the network addresses of the receiver.

    Import Statement

    Objective-C

    @import Foundation;

    Swift

    import Foundation

    Availability

    Available in OS X v10.0 and later.

  • Returns one of the hostnames of the receiver.

    Declaration

    Swift

    var name: String? { get }

    Objective-C

    @property(readonly, copy) NSString *name

    Return Value

    One of the hostnames of the receiver. Can be either a simple hostname, such as @"sales", or a fully qualified domain name, such as @"sales.anycorp.com".

    Import Statement

    Objective-C

    @import Foundation;

    Swift

    import Foundation

    Availability

    Available in OS X v10.0 and later.

  • Returns the name used as by default when publishing NSNetServices.

    Declaration

    Swift

    var localizedName: String? { get }

    Objective-C

    @property(readonly, copy) NSString *localizedName

    Return Value

    A string containing the computer name.

    Discussion

    This is the name displayed in the Finder sidebar, as well as in the Sharing preference panel.

    This method only returns an NSString when sent to the the currentHost instance, all other instances currently return nil.

    This property is key-value observable.

    Import Statement

    Objective-C

    @import Foundation;

    Swift

    import Foundation

    Availability

    Available in OS X v10.6 and later.

  • Returns all the hostnames of the receiver.

    Declaration

    Swift

    var names: [AnyObject] { get }

    Objective-C

    @property(readonly, copy) NSArray *names

    Return Value

    All the hostnames of the receiver.

    Import Statement

    Objective-C

    @import Foundation;

    Swift

    import Foundation

    Availability

    Available in OS X v10.0 and later.

  • Indicates whether the receiver represents the same host as another NSHost object.

    Declaration

    Swift

    func isEqualToHost(_ host: NSHost) -> Bool

    Objective-C

    - (BOOL)isEqualToHost:(NSHost *)host

    Parameters

    host

    Host to compare the receiver to.

    Return Value

    YEStrue when the receiver and host share at least one network address; NOfalse otherwise.

    Import Statement

    Objective-C

    @import Foundation;

    Swift

    import Foundation

    Availability

    Available in OS X v10.0 and later.

    See Also

    – addresses

  • Indicates whether caching is turned on or off.

    Deprecation Statement

    NSHost does not implement caching in OS X v10.6 and later.

    Declaration

    Objective-C

    + (BOOL)isHostCacheEnabled

    Return Value

    YEStrue when caching is turned on; NOfalse otherwise.

    Import Statement

    Objective-C

    @import Foundation;

    Availability

    Available in OS X v10.0 and later.

    Deprecated in OS X v10.7.

  • Specifies whether the receiver is to cache instances as it creates them to avoid creating duplicate instances.

    Deprecation Statement

    NSHost does not implement caching in OS X v10.6 and later.

    Declaration

    Objective-C

    + (void)setHostCacheEnabled:(BOOL)cacheOn

    Parameters

    cacheOn

    YEStrue to turn on caching. NOfalse to turn of caching.

    Discussion

    This method doesn’t flush the cache. If you turn caching off and then back on, new requests for hosts use what was in the cache at the time caching was turned off. However, NSHost objects created while caching is turned off aren’t entered into the cache.

    Import Statement

    Objective-C

    @import Foundation;

    Availability

    Available in OS X v10.0 and later.

    Deprecated in OS X v10.7.

  • Releases the cache of existing NSHost objects so subsequent requests for NSHost objects create new ones.

    Deprecation Statement

    NSHost does not implement caching in OS X v10.6 and later.

    Declaration

    Objective-C

    + (void)flushHostCache

    Discussion

    NSHost objects that were retained before this method was invoked remain valid.

    Import Statement

    Objective-C

    @import Foundation;

    Availability

    Available in OS X v10.0 and later.

    Deprecated in OS X v10.7.