access variable of another class - objective-c

How to access variables of other class? This is how I implemented it.
#interface Class1 :NSObject {
NSString *Data;
}
#property (nonatomic, retain) NSString *Data;
#implementation Class1
#synthesize Data;
someMethod{
self.Data = #"something";
}
and in Class2 :
#implementation Class2
someMethodOfClass2{
Class1 *c=[[Class1 alloc]init];
[c someMethod];
NSString *str=c.Data;
}
I get c.Data as null in Class2. Am I doing anything wrong here?
-----------myClass1--------------
#interface APIManager : NSObject {
NSString *Data;
}
#property (nonatomic, retain) NSString *Data;
-(void)getData;
#end
#implementation APIManager
#synthesize Data;
-(void)getData{
self.Data=#"response";
}
--------myClass2-------------
#interface Search : NSObject {
}
-(void)searchForItems:(NSString *)query;
#end
#implementation Search
-(void)searchForItems:(NSString *)query {
APIManager *apiManager=[[APIManager alloc]init];
[apiManager getData];
NSLog(#"%#",[apiManager Data]);
}

You should probably use self.Data = #"something" instead of self.Data = "something"

In Objective-C you have to use #"something" instead of "something". Also aren't you missing the variable declaration? In your #interface you should do something like NSString *Data.

Related

"No visible #interface" error with method

CJSHWork as a class is defined in CJSHWork.h and CJSHWork.m. In CJSHWork.h I have:
#interface CJSHWork : NSObject
#property (nonatomic, copy) NSString *name;
#property (nonatomic, copy) NSString *title;
#property (nonatomic, copy) NSString *url;
-(id) initWithName:(NSString *)name filename:(NSString *)title content:(NSString *)url;
#end
In CJSHWork.m I have:
- (id) initWithName:(NSString *)name title:(NSString *)title content:(NSString *)url
{
self = [super init];
if (self)
{
_name = name;
_title = title;
_url = url;
}
}
And I have, in CJSHDataController.m,
import "CJSHWork.h"
...
CJSHWork *work = [[CJSHWork alloc] initWithName:#"" title:allWorks[i][1] url:url];
This gets the error "No visible #interface for CJSHWork declares the selector initWithName title url".
What is going on here, and how can I fix it?
Your method name is initWithName:title:content: not initWithName:title:url:

Objective C serialize a collection to JSON/XML

I have a classes looking like this:
#interface AISlideItem: NSObject <NSCoding>
{
NSString* PlaceHolderName;
NSInteger PlaceHolderID;
}
#property (nonatomic, strong) NSString* PlaceHolderName;
#property (nonatomic) NSInteger PlaceHolderID;
#end
#interface AITitleSlideItem : AISlideItem
{
NSString* Title;
}
#property (nonatomic, strong) NSString* Title;
#end
#interface AIParagraphSlideItem : AISlideItem
{
NSMutableArray* Paragraphs;
}
#property (nonatomic, strong) NSMutableArray* Paragraphs;
#end
#interface AITextSlideItem : AISlideItem
{
NSString* Text;
}
#property (nonatomic, strong) NSString* Text;
#end
#interface AIPictureSlideItem : AISlideItem
{
NSMutableData* Picture;
}
#property (nonatomic, strong) NSMutableData* Picture;
#end
#interface AISlideContent : NSObject
{
NSString* LayoutName;
NSMutableArray* Items;
}
#property (nonatomic,strong) NSString* LayoutName;
#property (nonatomic,strong) NSMutableArray* Items;
#end
#interface ContentParaghraph : NSObject
{
NSInteger Level;
NSString* Text;
}
#property (nonatomic) NSInteger Level;
#property (nonatomic, strong) NSString* Text;
#end
I create a complex collection from these classes like this:
NSMutableArray* l = [[NSMutableArray alloc]init ];
AITitleSlideItem* tis1 = [[AITitleSlideItem alloc]init];
[tis1 setPlaceHolderName:#"I don't think we're using this..."];
[tis1 setPlaceHolderID:1];
[tis1 setTitle:#"This is a title"];
AITextSlideItem* tes1 = [[AITextSlideItem alloc]init];
[tes1 setPlaceHolderName:#"I don't think we're using this..."];
[tes1 setPlaceHolderID:2];
[tes1 setText:#"This is the description"];
AISlideContent* slide1 = [[AISlideContent alloc]init];
[slide1 setLayoutName:#"Title content"];
[[slide1 Items]addObject:tis1];
[[slide1 Items]addObject:tes1];
[l addObject:slide1];
[l addObject:slide1];
What i want to to serialize the NSMutableArray l into any portable format like json or xml.
Could you please post some code doing so?
Sincerely
Zoli
After further investigation i found nothing more suitable for my needs.
I decided to learn a bit more about JSON and create a function that creates the JSON structure manually. Even if it took 2-3 hours, it did worth all the effort.

Can I get NSString from other class?

well, I have 2 classes and want to get NSString from class2 to class1.
where do I wrong?
class1.h
#import <Cocoa/Cocoa.h>
#import "class2.h"
#class class2;
#interface class1 : NSObject
-(IBAction)getstringfromclass2(id)sender;
#end
class1.m
#import "class1.h"
#implementation class1
-(IBAction)getstringfromclass2(id)sender {
class2 *controller = [[class2 alloc] init];
NSLog(#"%#", [controller getstring]);
}
#end
class2.h
#import <Cocoa/Cocoa.h>
#interface class2 : NSObject {
NSString *astring;
}
-(NSString)getstring;
#property (readwrite,retain) NSString *astring;
#end
class2.m
#import "class2.h"
#synthesize astring;
#implementation class2
-(NSString)getstring {
return [self astring];
}
#end
check your some expression. NSString -> NSString *
#import <Cocoa/Cocoa.h>
#interface class2 : NSObject {
NSString *astring;
}
-(NSString *)getstring;
#property (readwrite,retain) NSString *astring;
#end
class2.m
#import "class2.h"
#synthesize astring;
#implementation class2
-(NSString *)getstring {
return [self astring];
}
#end
You need to return a pointer to NSString:
-(NSString*)getstring {
return [self astring];
}
And:
-(NSString*)getstring;

I'm having problems using a Singleton to pass an array in Objective-c. (code included)

So I'm creating an array called fruits which I would like to share between multiple views. This is my code:
#import <foundation/Foundation.h>
#interface MyManager : NSObject {
NSMutableArray *fruits;
}
#property (nonatomic, retain) NSMutableArray *fruits;
+ (id)sharedManager;
#end
#import "MyManager.h"
static MyManager *sharedMyManager = nil;
#implementation MyManager
#synthesize fruits;
#pragma mark Singleton Methods
+ (id)sharedManager {
#synchronized(self) {
if (sharedMyManager == nil)
sharedMyManager = [[self alloc] init];
}
return sharedMyManager;
}
- (id)init {
if ((self = [super init])) {
fruits = [[NSMutableArray alloc] init];
}
return self;
}
-(void) dealloc{
self.fruits = nil;
[super dealloc];
}
#end
Now I'm using the following code so that I can use workouts in the new view
#import "MyManager.h"
#interface Chest : UIViewController {
IBOutlet MyManager *fruits;
}
#property (nonatomic, retain) IBOutlet MyManager *fruits;
-(IBAction) goToList: (id) sender;
#end
When a button is clicked, goToList is called, and I populate my array, fruits
#import "Chest.h"
#implementation Chest
#synthesize fruits;
-(IBAction) goToList:(id)sender{
MyManager *fruits = [MyManager sharedManager];
NSString *filePath;
NSString *fileContents;
filePath = [[NSBundle mainBundle] pathForResource:#"chest_strength" ofType:#"csv"];
fileContents = [[NSString alloc] initWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
fruits = [fileContents componentsSeparatedByString:#"\n"];
}
When I output the elements of *fruits in this view, everything works fine. Now when I try to access this same variable in the other view, it says the array is null. Here's the code for the second view:
#interface List : UIViewController {
IBOutlet MyManager *fruits;
}
#property (nonatomic, retain) IBOutlet MyManager *fruits;
#end
#import "List.h"
#import "MyManager.h"
#implementation List
#synthesize fruits
NSLog(#"%#", fruits); //This is the part that isn't displaying correctly. I'm getting a null here
So my question is, how do I do this so that I can get the array fruits that I populated in Chest and use it in List so that I can display the contents of the array in that view?
Thank you to everybody who answers. This problem is seriously bogging me down and I need to finish this project ASAP. Much appreciated.
You've changed a few things in this question but the fundamental problem remains: the array is a property of your singleton, and you are not treating it as such.
Whenever you refer to the array, outside of the singleton, do it like this:
[MyManager sharedManager].fruits
If you are accessing it frequently, you can create a local ivar, but you certainly wouldn't have it as an IBOutlet, which you are doing in your last code sample. How is that ever going to be set?

iPhone Super/SubClass Example

I have a superclass Question:
import <Foundation/Foundation.h>
#import "Answer.h"
#interface Question : NSObject {
NSString* qId;
NSString* qTitle;
NSString* qNumber;
NSString* sectionId;
NSString* type;
Answer* answer;
}
#property (nonatomic, retain) NSString* qId;
#property (nonatomic, retain) NSString* qTitle;
#property (nonatomic, retain) NSString* qNumber;
#property (nonatomic, retain) NSString* sectionId;
#property (nonatomic, retain) NSString* type;
#property (nonatomic, retain) Answer* answer;
#end
#import "Question.h"
#implementation Question
#synthesize qId, qTitle, qNumber, sectionId, type, answer;
-(id)init
{
if (self = [super init])
{
// Initialization code here
answer = [[Answer alloc]init];
}
return self;
}
-(void) dealloc{
[answer release];
[super dealloc];
}
#end
I have several types of Question, one example is a Slider Question. I want this class to subclass Question:
#import <Foundation/Foundation.h>
#import "Question.h"
#interface SliderQuestion : Question {
(NSString*) min;
(NSString*) max;
}
#property (nonatomic, retain) NSString* min;
#property (nonatomic, retain) NSString* max;
#end
}
#import "SliderQuestion.h"
#implementation SliderQuestion
#synthesize min, max;
#end
Is this the correct way to subclass? Will SliderQuestion inherit the properties contained within Question?
SliderQuestion* s = [[SliderQuestion alloc]init];
NSLog(#"%#", s.qId); //is this valid
Do you really want min and max to be instances of NSString? It seems that floats would be more appropriate.
Also, scrap the () in (NSString *) to remove the warning/error message.
Finally, this is the appropriate way to subclass. An instance of SliderQuestion will inherit all properties and methods of the Question class (and NSObject as well)
Notice the ( and ) around the NSStrings, you might wanna remove those.