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

  • Home
  • SEARCH
  • 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 3499810
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T12:39:25+00:00 2026-05-18T12:39:25+00:00

I have a method (the code below is a simplified version) that parses small

  • 0

I have a method (the code below is a simplified version) that parses small text files:

- (void)parseFile:(NSString *)aFile
{
   NSDate *date;
   NSNumber *number;
   NSString *desc;

   NSString *txt = [NSString stringWithContentsOfFile:aFile encoding:NSUTF8StringEncoding error:nil];
   for (NSString *line in [txt componentsSeparatedByString:@"\n"]) {
      if ([linesubstring isEqual:@"mydate"]) {
         date = [dateFormat dateFromString:strDate];
      }

      if ([linesubstring isEqual:@"mynumber"]) {
         number = [numberFormat numberFromString:strValue];
      }

      if ([linesubstring isEqual:@"mydesc"]) {
         desc = [line substringWithRange:NSMakeRange(0, 10)];
      }

      if (!date && !number && !desc) {
         ...do something...
      }
   }
}

The first problem is that date variable is being filled with the content of aFile parameter. It only assumes it’s correct value, when the passes through the fist if/check.

So why? I though that date could be a reserved word and exchanged it, but with the same behavior.

The second problem is with the last if (with the nested ones). Debuging the code, i can see that xcode shows it as “out of scope”, but !number fails (xcode thinks that it’s valid)…

I tried other combinations, like [number isNotEqualTo:[NSNull null]] (this one throws an error EXC_BAD_ACCESS), without success.

Please, could anybody give some hints? I’m newbie with cocoa/objective-c. I’m coming from java…

TIA,

Bob

  • 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-18T12:39:25+00:00Added an answer on May 18, 2026 at 12:39 pm

    There’s quite a few things wrong with the code you’ve provided. I’m using the answer box because there isn’t enough room for this to be a comment:

    With regards to your variable declarations:

    NSDate *date;
    NSNumber *number;
    NSString *desc;
    

    You have correctly declared them, but you have not initialised them. As they are, they could be pointing to any random garbage. This means that your test at the end of the loop…

    if (!date && !number && !desc) {
       ...do something...
    }
    

    …may in fact always execute because date, number and desc may always be non-zero (I say may because it is actually undefined whether they are zero or non-zero). Initialise each of them to nil if you plan to determine whether they are set or not:

    NSDate *date = nil;
    NSNumber *number = nil;
    NSString *desc = nil;
    

    It is not always necessary to initialise variables (for example, as long as you write to it before you read from it, it is not necessary to initialise it), however some people promote the idea of initialising all variables to prevent this undefined behaviour from surfacing (I typically initialise all variables even if I overwrite the initialised value anyway).

    Also, there is a variable called linesubstring but it is not declared anywhere in the code, similarly strDate, strValue are not declared anywhere either. It is important to know how these are declared and how these are used as they may similarly be pointing to garbage.

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

Sidebar

Related Questions

I have two data structure classes (this is a simplified version of my code)
Bellow is simplified version of the code I have: public interface IControl<T> { T
I have the following C++ code (simplified version): class Shape { bool isCircle =
I have a problem understanding what's causes the compilation error in the code below:
Here's a simplified version of my needs. I have a program where every B
I have a WCF service that I secure with a custom UserNamePasswordValidator and Message
I have an enumeration like Enum Complexity { NotSoComplex, LittleComplex, Complex, VeryComplex } And
I have a form with a Clear button. When the user clicks Clear, I
I ran into a scenario where LINQ to SQL acts very strangely. I would
I'm wondering about the standard practice with inner classes (in Java but I suppose

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.