Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8165163
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T19:28:26+00:00 2026-06-06T19:28:26+00:00

I am having trouble with my Objective-C code. I am trying to print out

  • 0

I am having trouble with my Objective-C code. I am trying to print out all of the details of my object created from my “Person” class, but the first and last names are not coming through in the NSLog method. They are replaced by spaces.

Person.h: http://pastebin.com/mzWurkUL
Person.m: http://pastebin.com/JNSi39aw

This is my main source file:

#import <Foundation/Foundation.h>
#import "Person.h"

int main (int argc, const char * argv[])
{
Person *bobby = [[Person alloc] init];
[bobby setFirstName:@"Bobby"];
[bobby setLastName:@"Flay"];
[bobby setAge:34];
[bobby setWeight:169];

NSLog(@"%s %s is %d years old and weighs %d pounds.",
      [bobby first_name],
      [bobby last_name],
      [bobby age],
      [bobby weight]);
return 0;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T19:28:29+00:00Added an answer on June 6, 2026 at 7:28 pm

    %s is for C style strings (a sequence of chars terminated by a null).

    Use %@ for NSString objects. In general, %@ will invoke the description instance method of any Objective C object. In the case of NSString, this is the string itself.

    See String Format Specifiers.

    On an unrelated note, you should look into Declared Properties and @synthesize for your class implementation. It will save you a lot of typing as it produces all the getters and setters for you:

    person.h:

    #import <Cocoa/Cocoa.h>
    
    @interface Person : NSObject
    @property (nonatomic, copy) NSString *first_name, *last_name;
    @property (nonatomic, strong) NSNumber *age, *weight;
    @end
    

    person.m

    #import "Person.h"
    
    @implementation Person
    @synthesize first_name = _first_name, last_name = _last_name;
    @synthesize age = _age, weight = _weight; 
    @end
    

    main.m

    #import <Foundation/Foundation.h>
    #import "Person.h"
    
    int main (int argc, const char * argv[])
    {
        Person *bobby = [[Person alloc] init];
        bobby.first_name = @"Bobby";
        bobby.last_name = @"Flay";
        bobby.age = [NSNumber numberWithInt:34]; // older Objective C compilers.
    
        // New-ish llvm feature, see http://clang.llvm.org/docs/ObjectiveCLiterals.html
        // bobby.age = @34;
    
        bobby.weight = [NSNumber numberWithInt:164]; 
    
        NSLog(@"%@ %@ is %@ years old and weighs %@ pounds.",
          bobby.first_name, bobby.last_name,
          bobby.age, bobby.weight);
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having trouble referencing one view controller from another. The code works but I
I am having some trouble calling an objective-c class method from a C++ file.
I am having some trouble understanding designated initializers. I am studying Objective C from
this is a hw problem, ive done all the coding but im having trouble
I'm having trouble with Objective C. I'm trying to call a block after I've
I'm coming to Objective-C from a Java background. I'm having trouble understanding why the
I'm trying to understand CSS positioning and I'm having trouble figuring out why a
I am new to objective-c and am having trouble figuring out a way to
I am having trouble deciphering a passing argument ... from distinct Objective-C type warning.
I'm having a bit of trouble with memory leaks in my objective c code.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.