Challenge Solution


#1

Everything came out as I would have expected (I think), but would still prefer to post my code and ensure it is correct.

Thanks!

NSHost *host = [NSHost currentHost];

NSLog(@"The current host is %@", host);

NSString *localizedName = [host localizedName];

NSLog(@"The localized name is %@", localizedName);

#2

Looks good.


#3

This one was pretty simple!

[code]#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])
{

@autoreleasepool {
    
    // Create a NSHost object for this Mac
    NSHost *thisMac = [NSHost currentHost];
    
    // Get the name of this Mac
    
    NSString *macName = [thisMac localizedName];
    
    // Print the name of this Mac
    
    NSLog(@"The name of this Mac is %@", macName);
    
}
return 0;

}[/code]


#4

heres mine:

NSHost *myHost=[NSHost currentHost];
NSLog(@“the host name is: %@”,[myHost localizedName]);


#5

I seem to have a ponderous way of doing all this but here’s mine:

[code]#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])
{

@autoreleasepool 

{
    NSHost *myMac;
    myMac = [NSHost currentHost];
    NSLog(@"Your computer information is %@", myMac);
    
    NSString *myName;
    myName = [myMac localizedName];
    NSLog (@"Your name is %@", myName);
    
    

    
}

return 0;

}
[/code]


#6

my version - Your feedback is appreciated


#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])
{

    @autoreleasepool {
        
        // insert code here...
        NSHost *myMac = [NSHost currentHost];
        
        NSLog(@"Current host %@", myMac);
        
        NSString *name = [myMac localizedName];
        
        NSLog(@"My Mac's name is %@",name );
        
    }
    return 0;
}

#7

That looks good.

I would encourage you to use code comments, even with simple things like this as it will help as stuff gets more complicated.

I use the pseudocode technique where I first write out the steps I want to to in comments. Then I go back and fill in the lines of code for each section of comments. It helps me think about what the steps I actually need to code are, and it becomes automatic documentation in my code!


#8

I like @tahakhan solution!
It’s pretty much what I did (but I made a mistake naming the NSHost instance [damn you autocorrect]).
Is simple and to the point (and it works fast which is nice)


#9

//create new instance of NSHost NSHost *currentHost = [[NSHost alloc] init]; //get host name and store in computerName string NSString *computerName = [currentHost localizedName]; //send name to command line NSLog(@"%@", computerName);

Here’s mine.


#10

It seems that no one has any doubts about this part of the code:

NSHost *myMac; myMac = [NSHost currentHost]; NSLog(@"Your computer information is %@", myMac);
It’s fearless to print out myMac that way, because it’s of the type NSHost not NSString or any other well known type. How do you know, that this type supports printing like that ? I guess you will agree with me, that the result is a little bit unusual :unamused:

cu
Vertex


#11

Using “%@” indicates that the message description should be sent to the object, and since NSObject defines a default description method, it’s always safe to do so. Whether it’s useful is a different story.


#12

That’s what I wanted to point out - thanks for the explanation macintux, I think it’s helpful for everyone.

cu
Vertex


#13

This worked for me! Though I have to admit I’m a little fuzzy sometimes on the order of the class methods and instance methods and all that.

[code] NSString *comName = [[NSHost currentHost] localizedName];

    NSLog(@"The computer's name is %@.", comName);

[/code]


#14

[quote=“macshome”]That looks good.

I would encourage you to use code comments, even with simple things like this as it will help as stuff gets more complicated.

I use the pseudocode technique where I first write out the steps I want to to in comments. Then I go back and fill in the lines of code for each section of comments. It helps me think about what the steps I actually need to code are, and it becomes automatic documentation in my code![/quote]

Great technique, As a self taught programmer, I came to realize this process too. It is an excellent approach to programming.


#15

Since we only wanted to grab one piece of info from NSHost I did most of the work when I created an instance of NSString.

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])
{
    @autoreleasepool {
        NSString *name = [[NSHost currentHost] localizedName];
        NSLog(@"Current Host name is %@",name);
    }
    return 0;
}

#16

Here’s my solution:

        NSLog(@"The name of my Mac is: %@", [[NSHost currentHost] localizedName]);

#17

Here is mine:[code]#import <Foundation/Foundation.h>

int main(int argc, const char * argv[])
{

@autoreleasepool {
    
    NSHost *me = [NSHost currentHost];
    NSLog(@"My name is %@, here is some information about me %@",[me localizedName], me);
    
}
return 0;

}[/code]


#18

It’s fascinating to see how many variations of this can work.

I first did it with 3 lines:

NSHost *myMac = [NSHost currentHost];       
NSString *nameOfMac = [myMac localizedName];
NSLog(@"My Mac's name is %@", nameOfMac);

Then looking around on the forum gave me the idea for doing it in 2 lines:

NSHost *myMac = [NSHost currentHost];
NSLog(@"My Mac's name is: %@",[myMac localizedName]);

And eventually 1 line:

NSLog(@"My Mac's name is: %@",[[NSHost currentHost] localizedName]);

I am tempted to ask if one variation is preferable, but I am pretty sure the answer will be “it depends”…


#19

Here’s my solution:

[code]#import <Foundation/Foundation.h>

int main(int argc, const char * argv[])
{

@autoreleasepool {
    
    //Create an instance of NSHost called systemInfo and send it the message currentHost
    NSHost *systemInfo = [NSHost currentHost];
    
    //Create an instance of NSString called hostName to store the return of the message sent to the NSHost instance
    NSString *hostName = [systemInfo localizedName];
    
    NSLog(@"My host name is %@", hostName);
    
}
return 0;

}[/code]


#20

My solution

[code]#import <Foundation/Foundation.h>

int main(int argc, const char * argv[])
{

@autoreleasepool {
    
    NSHost *computer = [NSHost currentHost];            // Get computer info
    NSString *computerName = [computer localizedName];  // Get computer name
    NSLog(@"%@\n\n", computer);                         // Print computer info
    NSLog(@"%@", computerName);                         // Print computer name
    
}
return 0;

}[/code]