1
c++ code
--------
#include "stdafx.h"
#include <iostream>
using namespace std;
class A
{
public:
    static int a;
    void set(int s)
    {
     a=s;
     cout<<a<<endl;
    }
    void setData(int f)
    {
     cout<<"I am "<<f<<" years old!!!"<<endl;
    }
};
int A::a=0;
int main()
{
    A* ab=new A();
    ab->set(10);
    ab->setData(ab->a);
    return 0;
}

I am trying to get the same output for this equivalent Objective C code.

main.m
---------

#import <Foundation/Foundation.h>
#import "A.h"
int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    A* ab = [[A alloc]init];
    [ab set:10];
    [ab setData:ab.a]; //getting error when passed ab->a or ab.a as an argument
    [pool drain];
    return 0;
}

A.h
---

#import <Foundation/Foundation.h>

@interface A : NSObject {

}

-(void)set:(int)s;
-(void)setData:(int)f;

@end

A.m
----

#import "A.h"


@implementation A
static int a;
-(void)set:(int)s
{
    a=s;
    NSLog(@"%d\n",a);
}
-(void)setData:(int)f
{
    NSLog(@"%d",f);
}
@end

Error:Request for member 'a' in something not a structure or union.

2 Answers 2

2

There are no static instance variables or methods in Objective C. What you want can be done with class methods and static file scope variables. Class methods are those methods sent to class objects rather than instances.

@interface AClass
{
}

+(int) a;
+(void) setA: (int) newValue;

@end

// A.m

static int aStorage = 0;

@implementation AClass

+(int) a
{
    return aStorage;
}
+(void) setA: (int) newValue
{
    aStorage = newValue;
}

@end 

// To use:

    int something = [AClass a];
    [AClass setA: something * 2];

// Or dot syntax if you prefer

   AClass.a = AClass.a * 2;
Sign up to request clarification or add additional context in comments.

3 Comments

Being obtuse for the purposes of conversation — Beata, don't take this as an answer, it's a fun speculation of no practical use and describes a horrid hack with no benefits over the proper methods, hence being nothing anybody would ever actually want to use — given that the new runtime can add member variables at runtime, you've always been able to add methods at runtime and metaclasses are themselves classes, surely it'd be possible to create 'true' class variables nowadays?
@Tommy:You are right.I could not use the class variables with classname.variable .But i could able to access the methods the same way as JeremyP has done.I could able to get the output.Can you explain your statement with an example please.
@Beata: the example would be incredibly long-winded. It's likely the ObjC runtime could now allow class variables, but there's no syntax for declaring them. And the C-level runtime reference states that "Adding an instance variable to a metaclass is not supported" via its relevant function, so although something the internals likely support, it isn't exposed. Class variables are a little alien to the ObjC mindset so that isn't surprising. I just meant it as a fun diversionary thought.
1

First, declaring a static int in your implementation file doesn't magically make it a member of class A; your ObjC class A has no member variables.

Second, ab->a isn't how you would access the member of a class in ObjC. Once you have a member, write a getter for a, and use a method call to access it. (Or if you really want it to be static, don't add a member, and just add a getter that returns the static variable.)

Comments

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.