Chapter 14 Challenge Solution


#1

@autoreleasepool {

    // Instances of NSHost & NSString classes
    NSHost *host = [NSHost currentHost];
    NSString *computerName = [[NSString alloc] init];
    
    computerName = [host localizedName];
    
    NSLog(@"Computer name is \"%@\".", computerName);
    
}

#2
NSString *myComp = [[NSHost currentHost] localizedName];
NSLog(@"my comp name is: %@", myComp);

#3

NSHost *mycom = [[ NSHost alloc ] init];
NSString *naam = [ mycom localizedName];

    NSLog(@" De naam van mijn computer is \"%@\".", naam);

#4

[quote=“erikjan32”]NSHost *mycom = [[ NSHost alloc ] init];
NSString *naam = [ mycom localizedName];

    NSLog(@" De naam van mijn computer is \"%@\".", naam);[/quote]

Not sure that it would work in all cases in a future, cause in your case you just do alloc/init for NSHost object instance, and currentHost actually returns an NSHost object representing the host the process is running on.


#5

Thnx for the feedback but can you give an example of a case in which this does not work? Aren’t you allways running on a current host?


#6

I’m not sure, maybe someone more experienced could help us with that, but here is what I’ve found in the NSHost Reference document:


#7

Using alloc you make adequate or enough space for your object. Then using init you prepare it and something is happening behind the scene. Using init you initialize the object to a default value that every object initializes itself in this way. But because NSHost is related to a part of OS & it needs to know about a host that it is running on it you should you NSHost & this is design pattern of it.


#8

The thing is that if you do alloc-init instead of currentHost it still works as expected, giving you the host of your comp.
What we didn’t get is if there is a case when it wouldn’t work like this.


#9

Whenever in doubt, I would always read the class reference.


#10

[quote=“ibex10”]Whenever in doubt, I would always read the class reference.

[quote]
NSHost Class Reference

Overview

To create an NSHost object, use the currentHost, hostWithAddress:, or hostWithName: class methods [color=#FF0000](don’t use alloc and init)[/color]. 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.

[/quote][/quote]

Thanks, but if you look more closely I posted exactly the same quote from the reference a bit earlier in this very post.
So we get that it’s not recommended to use. What we don’t get is why exactly, cause in the example of the topic starter it works with alloc/init the same way mine works with currentHost.


#11

Unfortunately no documentation is perfect. In the absence of information, we just have to rely on common sense.

The reason it works with alloc/init is that most likely the init method is setting the internal host name to some meaningful default value, which is the current host, and that the currentHost method is probably doing an [[NSHost alloc] init].


#12

In this case it probably worked more because of what the instance method: localizedName does.

NSHost *myComputer = [NSHost currentHost];
NSLog(@"%@", myComputer);

NSHost *myMac = [[NSHost alloc] init];
NSLog(@"%@", myMac);

If you run the above code, you’ll see that *myComputer != *myMac. Actually when the NSLog line runs for myMac, this program will crash.


#13

nm. posted this in wrong place


#14

Hey,

this is what I came up with:

[code] NSHost *myHost = [[NSHost alloc] init];
myHost = [NSHost currentHost];

    NSString *myComputerName = [[NSString alloc] init];
    myComputerName = [myHost localizedName];
    
    NSLog(@"This computers localized name is: %@", myComputerName);[/code]

Somehow, looking at other solutions, I have the feeling that one or two lines of my solution are not needed.
Why is the line myHost = [NSHost currentHost]; not needed?

Thanks
Jay


#15

[quote=“jayzee”]

Somehow, looking at other solutions, I have the feeling that one or two lines of my solution are not needed.
Why is the line myHost = [NSHost currentHost]; not needed?

Thanks
Jay[/quote]

Because your first line does the same thing. However if you read the documentation it says not to use alloc/ init. So you would rewrite your code like this:

Then all you would need to do is use NSHost’s “localizedName” method to store the hosts name in an NSString.

Here is my solution:

 NSHost *myHost = [NSHost currentHost];
        NSString *myMac = [myHost localizedName];

        NSLog (@ "The name of this MacBook is: %@", myMac);