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 7084533
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:18:48+00:00 2026-05-28T07:18:48+00:00

I am just starting climbing the Objective C learning curve (using Nerd Ranch iOS

  • 0

I am just starting climbing the Objective C learning curve (using Nerd Ranch iOS programming book).

Based on what I have know from other languages about “nesting” multiple executions within one line I assumed that I can alter:

NSString* descriptionString = [[NSString alloc] initWithFormat:@"%@", possesionName] 

with a two line version:

NSString* descriptionString = [NSString alloc];
[descriptionString initWithFormat:@"%@", possesionName] 

but it seems that the second attempt raises an exception

2012-01-22 18:25:09.753 RandomPossessions[4183:707] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -length only defined for abstract class.  Define -[NSPlaceholderString length]!'

Could someone help me understand what exactly I am doing wrong here? Thanks a lot in advance.

PS. If this is a way Objective C messages work and you have to make alloc and init in one line just let me know – I assumed this is just a set of functions that either can be executed two in one go or one after another.

  • 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-05-28T07:18:48+00:00Added an answer on May 28, 2026 at 7:18 am

    The alloc method will allocate memory for a new object. But the init method might throw away that memory and return a completely different object. Or it might return nil. This is why you must always do self = [super init] when you override an init method.

    NSString is one class that does this kind of thing all the time.

    I’m not exactly sure why the exception is happening, but I believe it could be ARC injecting code in between your two lines of code or something similar. Whatever it is, something is trying to act on the allocated object that has never been initialised, and this is a huge problem that can lead to all kinds of issues. Consider yourself lucky it threw an exception, sometimes it wont.

    The NSString class might not actually be a real class. It may contain almost no methods and almost no variables. All it has is a bunch of factory methods to create “real” string objects of some other class, and this is done using methods like initWithFormat:. So, by long standing convention alloc/init must always be done in a single statement and there are a handful of places where, usually for performance reasons, something will rely on this convention being used.

    Basically, objective-c is a language where you don’t need to know exactly what is going on inside an object. You just need to know what messages can be sent to an object, and how it will respond. Anything else is undefined behaviour and even if you learn how it works, it is subject to change without notice. Sometimes the behaviour will change depending on circumstances that are completely illogical, for example you might expect the “copy” method to give you a copy of the object you send it to, and while this is the default behaviour, there are many cases where it will actually just return the same object with slightly different memory management flags. This is because the internal logic of the class knows that returning the same object is much faster and effectively identical to returning an actual copy.

    My understanding is copy sent to NSString may return a new object, or it may return itself. It depends on which NSString subclass is actually being used, and there isn’t even any documentation for what subclasses exist, let alone how they’re implemented. All you need to know, is that copy will return a pointer to an object that is perfectly safe to treat as if it was a copy even though it might not be.

    In a “proper” object oriented language like Objective-C, objects are “black boxes” which can intelligently change their internal behaviour at any time for any reason, but their external behaviour always remains the same.

    With regard to avoiding nesting… The coding style for Objective-C often does require extensive nesting, or else you’ll be writing 10 lines of code when only 1 is really needed. The square brace syntax is particularly suited to nesting without making your code messy.

    As a rule of thumb, I turn on Xcode’s “Page Guide at column” feature, and set it to 120 characters. If the line of code exceeds that width then I’ll think about breaking it into multiple lines. But often it’s cleaner to have a really long line than three short lines.

    Be pragmatic about it. 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Just starting using the Firebug console. I have a test script which I'll post
Just starting to learn Ruby on Rails. I'm using RoR 3. I have read
I just starting using VS2005 and I wish to have code highlighting in C/C++.
Just starting learning about ajax requests using jQuery. I've tried looking at other posts
Just starting out in asp.net. Have just created a login.aspx page in my site
Just starting out with subversion, have set up repos for 3 current projects and
Just starting to learn ASP.NET (C#) and I am using Visual Studio 2008. I
Im just starting with localization today and need some information. I have a project
Just starting out with an iPhone application using xcode 4.2. I understand that it
Just starting out with my first commercial app for the iStore but dont have

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.