AutoCAD objectC problem, what's wrong with the code?

New in objective-C,

#import <objc/objc.h>
#import <Foundation/Foundation.h>

@interface Test:NSObject
{
  int x,y, abc;
  NSString *v1, *v2;
}
@property int x , y, abc;
-(void) print;

@end

@implementation Test
@synthesize x,y, abc;
-(void) print
{
 NSLog (@"v1 and v2 values %i, %i ", v1, v2);
}

@end

int main ( int argc, char **argv)

{
  Test *t = [[Test alloc] init];
  /* Synthesized Set Method */
  [t setX:100];
  [t setY:200];
 /* Synthesized Get Method */
  NSLog (@"Retrieving Values %i, %i ",[t x], [t y]);

 /* another Way to retrieve the throuhg KVC Model */
 NSLog (@" KVC Retrieveal  %i ", [t valueForKey:@"x"]);

}

I did not get a compile-time error, but I got a runtime error:

2012-04-11 16:25:08.470 testpgm[22237] Retrieving Values 100, 200 
2012-04-11 16:25:08.513 testpgm[22237] autorelease called without pool for object (0x8e78ca0) of class NSMethodSignature in thread <NSThread: 0x8e23a08>
2012-04-11 16:25:08.514 testpgm[22237] autorelease called without pool for object (0x8e94610) of class NSIntNumber in thread <NSThread: 0x8e23a08>
2012-04-11 16:25:08.514 testpgm[22237]  KVC Retrieveal  149505552 

This seems to be due to a memory issue. Does anyone point out a problem?

NOTE. . Using all your inputs, I can solve the autorun problem, but still

NSLog (@" KVC Retrieveal  %i ", [t valueForKey:@"x"]);

does not print the correct value, but garbage. Am I doing something wrong?

+3
source share
4 answers

When you are in the application loop, a default autoplay pool has been created for you. However, when you work with your own main, you need to manually create an auto-resource pool at the top of yours mainand periodically clean it.

NSAutoreleasePool *myPool = [[NSAutoreleasePool alloc] init];
// Your code that uses autorelease...
[myPool drain];

LLVM, @autoreleasepool.

+4

.

.

ARC:

int main(int argc, char *argv[])
{
    @autoreleasepool {

    // your code

    }
}

int main(int argc, char *argv[]) {
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

     // your code

    [pool drain];
}

, :

NSLog (@"v1 and v2 values %i, %i ", v1, v2);

NSLog (@"v1 and v2 values %@, %@ ", v1, v2);

% @ ,% .

:

NSLog (@" KVC Retrieveal  %i ", [t valueForKey:@"x"]);

, valueForKey ( a NSNumber), :

NSLog (@" KVC Retrieveal  %@ ", [t valueForKey:@"x"]);

:

Retrieving Values 100, 200 
KVC Retrieveal  100 
+5

main the function must have an autostart pool.

int main(int argc, char *argv[])
{
    @autoreleasepool {

        // you code

        return ...;
    }
}

EDIT:
Regarding your second part of the question. valueForKeyreturns id, discards it onint

NSLog (@" KVC Retrieveal  %i ", [[t valueForKey:@"x"] intValue]);
+1
source
int main ( int argc, char **argv)

{
 NSAutoreleasePool *myPool = [NSAutoreleasePool new];
  Test *t = [[Test alloc] init];
  /* Synthesized Set Method */
  [t setX:100];
  [t setY:200];
 /* Synthesized Get Method */
  NSLog (@"Retrieving Values %i, %i ",[t x], [t y]);

 /* another Way to retrieve the throuhg KVC Model */
 NSLog (@" KVC Retrieveal  %i ", [t valueForKey:@"x"]);

 [pool drain];
}

maybe it will work

0
source

All Articles