Challenge (2nd Edition)

Hello,
I found next solution:

[code]@autoreleasepool {

    NSHost *host=[NSHost currentHost];
    NSString *name=[host localizedName];
    NSLog(@"The name of the computer %@",name);      
    
}
return 0;

[/code]

Here was my solution: NSHost *myHostName = [NSHost currentHost]; NSString *myHostNameLocalized = [myHostName localizedName]; NSLog(@"\nMy machine's name is %@.\n", myHostNameLocalized);

Here is my code for the second edition: :smiley:

#import <Foundation/Foundation.h>

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

    @autoreleasepool {
        
        NSHost *computerName = [NSHost currentHost];
        NSString *message = [computerName localizedName];
        NSLog(@"\n\nThe name of my computer is: %@\n", message);
    }
    return 0;
}

And here is mine, with the code compacted into a single line. I suppose this doesn’t really serve any purpose, becoming less extensible etc., but hey ho…

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

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

@autoreleasepool {
    
    // NSHost *host =[NSHost currentHost];
    
    //NSString *myName = [[NSHost currentHost] localizedName];
    
    NSLog(@"My name is %@", [[NSHost currentHost] localizedName]);// or replace with myName
    
}
return 0;

}
[/code]

Here is my solution

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

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

@autoreleasepool {
    NSHost *comp = [NSHost currentHost];
    NSString *info = [comp localizedName];
    NSLog(@"My Computer's name is %@.\n", info);
    
    
    
}
return 0;

}
[/code]

Here is another solution:

//  main.m

// MyComputer class provides information about my computer
//
@interface MyComputer : NSObject
+ (NSString *)name;
+ (NSString *)address;
@end

// Test drive MyComputer class
int main (int argc, const char * argv[])
{
    @autoreleasepool {
        NSLog (@"My Computer's name: %@\n", [MyComputer name]);
        NSLog (@"My Computer's  (IPv4 or IPv6) address: %@\n", [MyComputer address]);
    }
    return 0;
}

@implementation MyComputer

+ (NSString *)name
{
    return [[NSHost currentHost] localizedName];
}

+ (NSString *)address;
{
    return [[NSHost currentHost] address];
}

@end

This solution uses a helper class named MyComputer.
MyComputer is not only a class, it is also an object - a class object.
On a class object we can invoke only class methods.

[Accelerate your learning and become a competent programmer: pretty-function.org]

plz just post challenge solutions within the concepts shown in the book’s current chapter

FIle: main.m

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

@autoreleasepool {
    
    NSHost *host = [NSHost currentHost];
    NSString *computerName = [host localizedName];
    
    NSLog(@"Computer's name: %@", computerName);
    
}
return 0;

}
[/code]

Output:
2014-08-17 23:43:34.799 ComputerName[1781:303] Computer’s name: tpb’s Mac Pro
Program ended with exit code: 0

Greetings everyone.

I did it, and it is just like everyone else’s. I added comments just to make sure I understood what I did.
Here it is…[code]// NSHost

#import <Foundation/Foundation.h> /* NSHost, NSString, NSLog */

int main(int argc, const char * argv[])
{
@autoreleasepool
{
NSHost here = [NSHost currentHost]; / evoke the currentHost method (a class method) /
/
on NSHost (a class object) that exists at the /
/
address held in the pointer *here */

    NSString *name = [here localizedName]; /* evoke the localizedName method (an instance method) */
                                           /* on NSHost (a class object). The localizedName method */
                                           /* returns a pointer to an instance of NSString that */
                                           /* exists at the address held in the pointer *name. */
  
    NSLog(@"My computer's name is %@.\n", name); /* NSLog prints out my computers name */
                                                 /* held in the address 'name'. */
  }

return 0;

}[/code]
The out put is [quote]2014-10-27 16:53:57.636 NSHost[51081:303] My computer’s name is JackRabbit’s iMac.[/quote]
Speaking of the output. Once upon a time you could get rid of the timestamp and just get the output of your program by switching All Output to Target Output in that little toggle in the lower left of the console pane. Now this can’t be done! All Output ,Target Output all gives the same output. Is there a way to get rid of this so I just see what I want to see?

[color=#004040]JR[/color]

[quote] NSHost *here = [NSHost currentHost]; /* currentHost is a class method sent to NSHost */ ... [/quote]
Using the standard language: Invoke the currentHost method (a class method) on NSHost (a class object) or send the currentHost message to NSHost.

Done. I changed the documentation in my Challenge example to the wording you suggested.

Normally I am a nonconformist by nature, but when I decided to learn programming, I saw the importance of using a standardized language (beyond the actual code), using a standardised language when discussing code and program design. At the beginning of the book that this forum surrounds (You and This Book p.5) the author suggested reading “Dreaming in Code”. In reading it, it underscored the necessity for this need for exact language so people can work together as seamlessly as possible.

Obviously ibex10, you are deeper in the discipline than a lot of us here on this forum. And I imagine you are in the middle of a project as we speak. I must say that I appreciate the input you offer. If we didn’t have this input from you and others like you, it might be a case of the blind leading the blind. One day when I understand the technology, I hope to do the same.

Oh, and before I finish this post, any thoughts on the All Output/Target Output problem I mentioned in my above post?

[color=#004040]JR[/color]

Oh this is way beyond me ibex10, so I’ll just take a small nibble.

+ (NSString *)name;

This code is dealing with the name of the host computer but the plus sign and the (NSString *) confounds me. Is (NSString *) a type of pointer? What about the plus sign (+)? Does it indicate we are dealing with a class?

[color=#004040]JR[/color]

First of all, thank you for your kind words. When writing code, using a clear, precise, and concise language is very important, just as in any language used for communication. Sometimes, when I look at code I have written in the past, I curse myself for not making the effort.

Now back to the question.

[quote]+ (NSString *)name;
This code is dealing with the name of the host computer but the plus sign and the (NSString *) confounds me. Is (NSString *) a type of pointer? What about the plus sign (+)? Does it indicate we are dealing with a class?
[/quote]
That’s a class method, which returns a Cocoa string object (NSString *), an instance of NSString class.

Of course, you know that a class method can be invoked only on its class object.

For example, you will see the following pattern occurring very often in Cocoa or iOS code written in Objective-C:

NSMutableString * input = [NSMutableString string]; // create a mutable empty string
...
NSMutableArray * tokens = [NSMutableArray array];

You’re most welcome.

Now back to the question…NSMutableString * input = [NSMutableString string]; // create a mutable empty string ... NSMutableArray * tokens = [NSMutableArray array]; What would the documentation of the second line of code be?

[color=#004040]JR[/color]

Oops. That should have been something like create a mutable (empty) array.

But that’s stating the obvious. It would be much better to describe what they are for:

// String to store our input to parse
NSMutableString * input = [NSMutableString string];

// Container to store our tokens
NSMutableArray * tokens = [NSMutableArray array];

[quote=“ibex10”]Oops. That should have been something like create a mutable (empty) array.

But that’s stating the obvious. It would be much better to describe what they are for:

[code]
// String to store our input to parse
NSMutableString * input = [NSMutableString string];

// Container to store our tokens
NSMutableArray * tokens = [NSMutableArray array];
[/code][/quote]

For a guy like me, at this stage of the game ibex10, the obvious can be a comforting thing.

[color=#004080]JR[/color]

Here’s my solution to this challenge.

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

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

}[/code]

Notice my second line of code [getComputerName localizedName]; I was able to write it out without creating a second variable to store it.

[quote][code]int main(int argc, const char * argv[]) {
@autoreleasepool {

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

}[/code]

[/quote]
That only prints the description of the current host, not its name.

Also, the variable getComputerName is confusing. Because you are using it to denote an NSHost object, you will make your code’s intention clearer if you name the variable appropriately (for example: myComputer or currentHost).

To print the name of the computer, do this:

int main (int argc, const char * argv[]) {
    @autoreleasepool {
       
        NSHost * myComputer = [NSHost currentHost];
        
        NSLog (@"My computer's name is %@", [myComputer localizedName]);
    }

Or more verbosely:

int main (int argc, const char * argv[]) {
    @autoreleasepool {
       
        NSHost * myComputer = [NSHost currentHost];
        NSString * nameOfMyComputer = [myComputer localizedName];

        NSLog (@"My computer's name is %@", nameOfMyComputer);
    }

Or tersely, without using any variables:

int main (int argc, const char * argv[]) {
    @autoreleasepool {
        NSLog (@"My computer's name is %@", [[NSHost currentHost] localizedName]);
    }

I came up with the following for the challenge.

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

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

    NSHost *computerName = [NSHost currentHost];  
    NSString *host = [computerName localizedName];
    
    NSLog(@"The name of this computer is %@.", host);
}
return 0;

}
[/code]