开发者

Problem With method with different classes

开发者 https://www.devze.com 2023-02-10 19:42 出处:网络
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 开发者_开

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...


...

+ (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);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消