I'm having a problem with NSString. I feel like it's a pretty simple thing to figure out but I've been staring at it for a while and just can't seem to get anywhere :/ Your help is highly appreciated!
I defined a class called Painting.
Here is Painting.h:
#interface Painting : NSObject {
NSString *artist;
UIImage *image;
}
#property (nonatomic, copy) NSString *artist;
#property (nonatomic, copy) UIImage *image;
#end
Here is Painting.m:
#implementation Painting
#synthesize artist, image;
#end
In a .h file for a view controller, I create a "painting":
Painting *monet;
#property (nonatomic, retain) Painting *monet;
Now, in it's .m file, I'm trying to do a very very simple print and it won't work. I get (null) instead of "Monet".
monet.artist = #"Monet";
NSString *bob3 = monet.artist;
NSLog(#"Real artist: %#", bob3);
However, this does work (gives me "Monet" instead of (null)):
NSString *bob3 = #"Monet";
NSLog(#"Real artist: %#", bob3);
What am I missing??
You probably need before:
monet.artist = #"Monet";
this:
monet = [[Painting alloc] init];
Related
In my iPhone App I have ViewController1 that contains a UILabel called selectedBook and I added to its view in the storyboard and I connected the outlets.
I have another view controller called ViewController2 and I pass some data from ViewController2 to ViewController1 and the text of the UILabel is one of them.
In ViewController1 I wrote the following code:
in ViewController1.h
#interface ViewController1 : UIViewController{
IBOutlet UILabel *selectedBook;
NSString *BookName;
NSString *BookCover;
NSString *BookAbout;
}
#property (nonatomic, retain) NSString *BookName;
#property (nonatomic, retain) NSString *BookCover;
#property (nonatomic, retain) NSString *BookAbout;
-(void) setTheSelectedBook:(NSString *)bookName bookCover:(NSString *)bookCover bookAbout:(NSString *)bookAbout userID:(int)userID;
In ViewController1.m
-(void) setTheSelectedBook:(NSString *)bookName bookCover:(NSString *)bookCover bookAbout:(NSString *)bookAbout userID:(int)userID
{
BookName = bookName;
BookCover = bookCover;
BookAbout = bookAbout;
NSLog(#"in setTheSelectedBook, BookName: %# - BookCover: %# - BookAbout: %# - userID: %d", BookName, BookCover, BookAbout, userID);
selectedBook.text = bookName;
}
In viewController2 I wrote the following code:
ViewController1 *viewController = [self.storyboard instantiateViewControllerWithIdentifier:#"Suggest"];
[viewController setTheSelectedBook:#"MybookName" bookCover:#"MyBookCover" bookAbout:#"MuBookAbout" userID:1];
My data pass properly and I see it in the output, but the problem is that the UILabel text is not updated, meaning that this line
selectedBook.text = bookName;
does not work.
I tried writing it as selectedBook.text = #"someText"; and this works.
I can't understand what's missing to make selectedBook.text = bookName; work? I hope someone help me in this issue.
Thanks in advance.
Your code is a bit confusing. Never begin your variables with a capital letter. BookName should be bookName or _bookName. I think that's one of the problems.
If you instead create a Book-class with the variables:
#property (copy) NSString *name;
#property (copy) NSString *cover;
#property (copy) NSString *about;
Then in your viewController1 create the book by instantiating your Book-class and set it's variables, you could then send your Book-object to your viewController2.
That would probably be easier for you.
If you just want to change your own code, you could try with:
In your h-file:
#property (copy) NSString *BookName;
#property (copy) NSString *BookCover;
#property (copy) NSString *BookAbout;
And in your m-file:
-(void) setTheSelectedBook:(NSString *)bookName bookCover:(NSString *)bookCover bookAbout:(NSString *)bookAbout userID:(int)userID
{
_BookName = bookName;
_BookCover = bookCover;
_BookAbout = bookAbout;
NSLog(#"in setTheSelectedBook, BookName: %# - BookCover: %# - BookAbout: %# - userID: %d", BookName, BookCover, BookAbout, userID);
selectedBook.text = _BookName;
}
The only explanation I see here is, that bookName is not an NSString.
Add the following line to that method:
NSLog(#"bookName class is %#", [bookName class]);
NSLog(#"selectedBook class is %#", [selectedBook class]);
What does XCode write to the log during runtime?
I am trying to get image in return from the instance method, i am declaring the method in following way, but giving error. I am using NSObject class.
-(UIImage *)getAdImg;
This is giving error that "Expected a type". What does it mean. I normal view controller it is working fine, but in NSObject class it is giving this error. Please guide for the above.
Thanks in advance.
This is my .h file
#import <Foundation/Foundation.h>
#import "JSON.h"
#interface adClass : NSObject
{
NSString *mStrPid;
NSString *mStrAppUrl;
}
#property (nonatomic, strong) NSString *mStrPid;
#property (nonatomic, strong) NSString *mStrAppUrl;
#property (nonatomic, strong) NSMutableArray *mArrAdsDesc;
#property (nonatomic, strong) NSMutableArray *mArrDeviceType;
#property (nonatomic, strong) NSString *mDevice;
#property (nonatomic, strong) NSString *mImgStr;
-(void)iphoneDevice;
-(UIImage *)getAdImg; //(error line)
#end
You have to #import <UIKit/UIKit.h> (where UIImage is declared)
Use this code
-(UIImage *)getAdImg
{
UIImage *image=[[UIImage alloc]init];
image.images=/**An Array of images**/
return ima;
}
Hope it helps!!!
I am pushing a detail view from the master of a Master-Detail Application template.
I want to load a single view with different information depending on which row was clicked. I plan to load the data from a plist file.
Here is the code I used in the MasterViewController, when I added didSelectRowAtIndexPath:
DetailViewController *dvc = [[DetailViewController alloc]init];
dvc.rowItem = [_objects objectAtIndex:indexPath.row];;
NSLog(#"row is %i", indexPath.row);
NSLog(#"master %#", dvc.rowItem);
where rowItem is an NSString belonging to the DetailViewController. Inside the viewDidLoad of the DetailViewController I put:
[super viewDidLoad];
NSLog(#" thingy %#", rowItem);
[self setup:rowItem];
and setup looks like this:
-(void) setup: (NSString *)eval {
filePath = [NSString stringWithFormat:#"%#.plist",eval];
NSLog(#"%#", filePath);
NSMutableDictionary* plistDict = [[NSMutableDictionary alloc] initWithContentsOfFile:filePath];
NSString *subName;
NSString *yesNo;
NSString *theseChanges;
subName = [plistDict objectForKey:#"subContractAddress"];
yesNo = [plistDict objectForKey:#"yesOrno"];
theseChanges = [plistDict objectForKey:#"Changes"];
}
Here is my interface:
#import <UIKit/UIKit.h>
#interface DetailViewController : UIViewController
#property (strong, nonatomic) id detailItem;
#property (strong, nonatomic) IBOutlet UILabel *detailDescriptionLabel;
#property (strong, nonatomic) IBOutlet UIButton *finishedButton;
#property (strong, nonatomic) IBOutlet UITextView *changes;
#property (strong, nonatomic) IBOutlet UIButton *saveButton;
#property (strong, nonatomic) IBOutlet UISwitch *test;
#property (strong, nonatomic) IBOutlet UILabel *finishedLabel;
#property (strong, nonatomic) IBOutlet UITextField *subContractName;
#property BOOL needsChanges;
#property (strong, nonatomic) NSString *rowItem;
-(IBAction)change:(id)sender;
-(IBAction)save:(id)sender;
#end
when I build and run I get the console output of:
thingy (null)
(null).plist
row is 0
master Excavations
when I press the first row, and
thingy (null)
(null).plist
row is 1
master Flooring
so somehow, rowItem is getting set to null in the crossover.
Can someone see what I am doing wrong here?
Try changing your two log statements as follows:
NSLog(#"master %#, detail %#", dvc.rowItem, dvc);
NSLog(#" thingy %# in detail %#", rowItem, self);
I suspect that the detail you create as dvc is not the same object that is in the view controller hierarchy. Showing the object addresses should verify whether that's the answer.
Do your setup in "viewWillAppear" and I'll bet it starts working.
Please check with this,
dvc.rowItem = [[NString alloc]initWithString:#"%#",[_objects objectAtIndex:indexPath.row]];
I have a problem accessing a object in my array. I store "Place" objects in my NSMutableArray. I want to access this array for my TableView. I get the "No known instance method for selector" error in line one. See lines below.
cell.imageView = [[self.currentPlaces objectAtIndex:indexPath.row]picture];
cell.subtitleLB.text = [[self.currentPlaces objectAtIndex:indexPath.row]description];
cell.objectNameLB.text = [[self.currentPlaces objectAtIndex:indexPath.row]name];
This is my Place object:
#interface Place : NSObject{
CLLocation *objectLocation;
UIImageView *picture;
NSString *name;
NSString *description;
}
The access of the properties "description" and "name" is no problem. I just dont know why this error occurs.
Thx. Dominik
I had the same problem; what worked for me was passing the UIImage instead of the UIImageView. So your code should look like this:
#interface Place : NSObject{
CLLocation *objectLocation;
UIImage *picture;
NSString *name;
NSString *description;
}
and this
cell.imageView.image = [[self.currentPlaces objectAtIndex:indexPath.row]picture];
cell.subtitleLB.text = [[self.currentPlaces objectAtIndex:indexPath.row]description];
cell.objectNameLB.text = [[self.currentPlaces objectAtIndex:indexPath.row]name];
If that doesn't work I'll post some more code for you to look at.
You haven't actually declared any methods. What you have declared are instance variables. You should probably be using #propertys instead.
#interface Place : NSObject
#property (nonatomic, retain) CLLocation *objectLocation;
#property (nonatomic, retain) UIImageView *picture;
#property (nonatomic, copy) NSString *name;
#property (nonatomic, copy, getter=objectDescription) NSString *description;
#end
This will actually create the methods that you want. Note that I changed the method for the description property to read -objectDescription. This is because NSObject already declares the -description method and you shouldn't be overriding it with an unrelated property.
If you're on recent Clang, then this is all you need, and instance variables will get synthesized automatically (using an underbar prefix, e.g. _picture). If you're on an older version (e.g. if this causes errors), you need to add #synthesize lines, as in
#implementation Place
#synthesize objectLocation=_objectLocation;
#synthesize picture=_picture;
#synthesize name=_name;
#synthesize description=_description;
#end
I have a problem that I have been searching for a solution for but can't seem to find one that works. UserInput.xib , Calculations.h & .m ,DataOutput.xib ... there is no .xib for that calculations.
UserInput.h
DataOutput *dataOutput;
UITextField *tf1;
UITextField *tf2;
UITextField *tf3;
#property (nonatomic, retain) DataOutput *dataOutput;
#property (nonatomic, retain) IBOutlet UITextField *tf1;
#property (nonatomic, retain) IBOutlet UITextField *tf2;
#property (nonatomic, retain) IBOutlet UITextField *tf3;
UserInput.m
#synthesize dataOutput;
#synthesize tf1;
#synthesize tf2;
#synthesize tf3;
- (IBAction)calculate:(id)sender {
DataOutput *dataOut = [[DataOutput alloc] initWithNibName:#"DataOutput" bundle:nil];
Calculations *calc = [[Calculations alloc] init];
dataOut.dataCalc = calc;
dataOut.dataCalc.tf1 = tf1.text;
dataOut.dataCalc.tf2 = tf2.text;
dataOut.dataCalc.tf3 = tf3.text;
dataOut.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentModalViewController:dataOut animated:YES];
[dataOut release];
DataOutput.h
Calculations *dataCalc;
UILabel *results1;
UILabel *results2;
UILabel *results3;
#property (nonatomic, retain) Calculations *dataCalc;
#property (nonatomic, retain) IBOutlet UILabel *results1;
#property (nonatomic, retain) IBOutlet UILabel *results2;
#property (nonatomic, retain) IBOutlet UILabel *results3;
DataOutput.m
#synthesize results1;
#synthesize results2;
#synthesize results3;
- (void)viewDidLoad {
self.results1.text = dataCalc.tf1;
self.results2.text = dataCalc.tf2;
self.results3.text = dataCalc.tf3;
Calculations.h
NSString *tf1;
NSString *tf2;
NSString *tf3;
NSString *results1;
NSString *results2;
NSString *results3;
float *tempResults1;
float *tempResults2;
float *tempResults3;
#property (nonatomic, retain) NSString *tf1;
#property (nonatomic, retain) NSString *tf2;
#property (nonatomic, retain) NSString *tf3;
#property (nonatomic, retain) NSString *results1;
#property (nonatomic, retain) NSString *results2;
#property (nonatomic, retain) NSString *results3;
- (float)getResults1;
- (float)getResults2;
Calculations.m
#synthesize tf1;
#synthesize tf2;
#synthesize tf3;
#synthesize results1;
#synthesize results2;
#synthesize results3;
- (float) getResults1 {
float temp1 = [tf1 floatValue];
float temp2 = [tf2 floatValue];
if (temp1 <= 1 && temp2 >= 3) {
if (temp2 ==10) {tempResults1 = 50;}
else if (temp2 == 11) {tempResults1 = 52;}
etc etc etc ok here is my problem..with the way I have things setup I can carry data from the userInput, threw Calculations and display them in a label on the DataOutput. BUT, when
using the floats in those if statements on calculations.m that I declared in calculations.h... I can't carry the floats (the actual data calculations) over to the DataOutput screen. on DataOutput when I try to set it a float from calculations it doesn't recognize it, it will recognize the NSString but not the float. I have tried converting float tempResults1 to NSString results1 and i keep getting errors. I have tried several different ways to go about doing this from different questions and answers on here but can't figure out why it wont work. can anyone help me with this?
What I want to do is to be able to display results from the calculations on the dataoutput screen.
I know it has to be something simple, maybe I'm doing something wrong or overlooking looking something I didn't do ... I don't know, I could use a little guidance though I know that much.
if (temp1 <= 1 && temp2 >= 3) // this is ok
{
if (temp2 ==10) { // exact floating point comparisons are dangerous,
// and should be avoided. you must rewrite this.
// turn up your compiler warnings
tempResults1 = 50; // this is not what you think it is.
// turn up your compiler warnings. you
// are assigning the address for the
// pointer's value. this will lead to a
// crash after you use it. what exactly
// are you trying to accomplish with this
// statement? this must be rewritten.
}
else if (temp2 == 11) {tempResults1 = 52;} // as above