0

i have a problem, i have this classes:

MainViewController.m
MainViewController.h
Myapp.m
Myapp.h

i want use a method "restanswer" declared in Myapp.m in MainViewController.m, this is the code:

//MyApp.h @class MainViewController;

@interface MyApp : DEFINE_SUPERCLASS // << @todo note to OP: define your superclass. you rarely need to create a root class in objc.
{
  NSMutableArray * answer;
}

@property (nonatomic, retain) NSMutableArray *answer;
- (NSMutableArray *) restarray;

@end

//MyApp.m
#import "MainViewController.h"

@implementation Myapp

@synthesize answer;

NSMutableArray * answer = nil;

- (NSMutableArray *)restarray {
  answer = [[NSMutableArray alloc] initWithObjects:@"1", @"2",@"3", nil];
  return answer;
}

//MainViewController.m
#import "MyApp.h"


@implementation MainViewController

@synthesize answer;

static Myapp * risposte;

-(void).......{
  NSMutableArray * prova = [risposte restarray];
  int numbertest = [prova count];
  NSLog(@"the value is: %d", numbertest);
}

i have no error, but the value of numbertest is: 0, why? my array have 3 object, please help me...sorry for format code i try but don't work...

3
  • have you created 'riposte' object - check may be its value is nil?.. Commented Feb 14, 2011 at 0:26
  • 1
    note: NSMutableArray * answer = nil; probably does not do what you think it does. it declares a new answer, and has nothing to do with the instance variable answer Commented Feb 14, 2011 at 0:49
  • stackoverflow.com/tags/xcode/info Commented Feb 14, 2011 at 9:54

1 Answer 1

1
...

+ (MyApp *)sharedRiposte
{
// ok -- your OP is lacking (requested) detail
// i have to infer some things:
// 1) MyApp is an NSApplication or UIApplication subclass
// 2) your program actually has designated MyApp as the app's type

 --- if OS X ---
   MyApp * app = (MyApp*)[NSApplication sharedApplication];
   if (![app isKindOfClass:[MyApp class]]) {
     assert(0 && "oops, the app type is not defined correctly");
     return nil;
   }
   else {
     return app;
   }
 --- if iOS ---
   MyApp * app = (MyApp*)[UIApplication sharedApplication];
   if (![app isKindOfClass:[MyApp class]]) {
     assert(0 && "oops, the app type is not defined correctly");
     return nil;
   }
   else {
     return app;
   }
}

-(void).......{
  MyApp * riposte = [[self class] sharedRiposte];
  assert(risposte && "oops, app is not configured properly (assuming MyApp is an NS/UI-Application subclass)");

  NSMutableArray * prova = [risposte restarray];
  assert(prova && "oops, risposte could not create the restarray");

  int numbertest = [prova count];

  // we know the answer to this based on the *current* implementation of restarray
  assert(3 == numbertest && "oops, the array is not what we expect");

  NSLog(@"the value is: %d\nthe array is: %@", numbertest, prova);
}
Sign up to request clarification or add additional context in comments.

5 Comments

thank you for the answer, can you correct the code please?...without that assert :), how i can initialize risposte?, and why risposte could not creat the restarray?
@Piero you don't remove the assertion. the point is not to remove asserts/checks which fail (simply) because they fail. they must succeed for your program to function as intended. the point is to assure your program is correct, and to correct the asserts/checks which fail :) your OP is lacking way to much detail for me to answer without guessing - i had to guess what you're trying to do here, but i updated my response. remember: don't remove the asserts, understand why they fail, then correct the problems with your program so the asserts don't fail. good luck.
i try but don't work :(...i'll do informatic university, and i'm able to write in java, very well, but Object-C no, how i can study it?...can u suggest me a good book?...where i can also learn the question i made here?
@Piero did you see this comment (i edited your post)? DEFINE_SUPERCLASS // << @todo note to OP: define your superclass. you rarely need to create a root class in objc.
@Piero as for learning objc having a java background: i recommend learning basic C first. java hides much of the lower levels of C from developers. a C foundation is necessary when writing in objc. after that, your combination of java+C knowledge will make much of the remainder simple. objc is a superset of C. if you want a specific objc book, try Hillegass.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.