Beginner struggling with Objective-C - objective-c

I'm following an objective-c book (objective-c fundamentals by Fairbairns, Fahrenkrug, Ruffenach), and I've fallen at the first hurdle with their CoinToss example.
I'm getting an 'expression expected' error on this line:
result.text = coinLandedOnHeads ? #"Heads" : #"Tails";
I have also included a screenshot of the whole page below.
What exactly is the problem? I've checked and double checked the code is the same as the book, but have I missed something very obvious?
Thanks!
EDIT:
Here is my header file:
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController {
UILabel *status;
UILabel *result;
}
#property (nonatomic, retain) IBOutlet UILabel *status;
#property (nonatomic, retain) IBOutlet UILabel *result;
-(IBAction)callHeads;
-(IBAction)callTails;
#end

I know your problem exactly.
The book is outdated. When you created the project you had the tick box 'Automatic reference counting' selected. That means, you don't have to do the dealloc. It's not your fault, Xcode just has a new automatic memory management capability, and the book is old enough so that it's making you do it manually.
To fix it:
Remove the dealloc method entirely
Or,
Restart your project and don't tick the 'Automatic Reference Counting' tick box.

Related

xcode objective c missing #end on NSObject

I've been racking my brains for the past 2 hours regarding this issue and I couldn't find any solution through searching or by removing the imported files of my CustomTableCell.
Here's my class (.h)
#import <UIKit/UIKit.h>
#interface MatchTableCell : UITableViewCell{
}
#property (nonatomic, weak) IBOutlet UILabel *matchId;
#property (nonatomic, strong) IBOutlet UILabel *fighter1Name;
#property (nonatomic, weak) IBOutlet UILabel *fighter2Name;
#property (nonatomic, weak) IBOutlet UILabel *status;
#end
This is what's popping up:
Missing '#end' .. this is popping up on the #interface line
Expected Identifier or '(' .. this highlights the first property
Thoughts?
EDIT: Solution. Apparently I can't post this as an answer even though that's how I solved the issue. Anyway, just in case it helps anyone, check the imports of the ViewControllers (or objects) that imports the file getting the error
Well, that was very tedious. Found a ViewController that is imported by a second viewcontroller, this second viewcontroller imports this NSObject. The first viewcontroller, for some reason got changed from UITableViewController to UIViewController. Thanks to #rob-mayoff for the idea
You are missing the #end directive at the end of one of your other .h files. Go through the .h files in your project one by one. For each one that has an #interface or #protocol section, make sure the section is terminated with an #end directive.
Also to mention that start with checking the recently added files. XCODE starts to show this error any where but doesn't indicate which file is missing the combination of #interface and #end. One of the file will have it missing.

Error: Expected ; at end of declaration list - Class not recognized as type

Here is my viewcontroller.h file:
#import <UIKit/UIKit.h>
#import <CoreData/CoreData.h>
#import <CoreLocation/CoreLocation.h>
#import <AVFoundation/AVFoundation.h>
#import <CoreMedia/CoreMedia.h>
#import <ImageIO/ImageIO.h>
#import <CoreVideo/CoreVideo.h>
#interface ViewController : UIViewController <UINavigationControllerDelegate, CLLocationManagerDelegate>
#property (nonatomic, strong) NSManagedObjectContext *managedObjectContext;
#property(nonatomic, weak) IBOutlet UIImageView *selectedImageView;
#property(nonatomic, retain) IBOutlet UIImageView *vImage;
#property(nonatomic, retain) AVCaptureStillImageOutput *stillImageOutput;
- (IBAction)photoFromAlbum;
- (IBAction)photoFromCamera;
- (IBAction)saveImageToAlbum;
- (IBAction)segueToChoosePhotoTypeViewController;
#end
The line:
#property(nonatomic, retain) AVCaptureStillImageOutput *stillImageOutput;
is generating the following issue:
Expected ; at end of declaration list.
Xcode isn't recognizing AVCaptureStillImageOutput as a class as it remains black and doesn't suggest it as a type, and the "Fix-it" wants to insert the ; right after AVCaptureStillImageOutput.
However, if I try to use declare AVCaptureStillImageOutput as a type in viewDidLoad of my viewController.m file, it recognizes it and allows it as a type. Also, the rest of the AVFoundation classes are being recognized in the code there.
The framework is there, I'm just having an issue with this #property declaration.
In your line
#property(nonatomic, retain) AVCaptureStillImageOutput *stillImageOutput;
you have some invisible characters between AVCaptureStillImageOutput and *stillImageOutput. If I paste this line into the vi editor, it looks like this:
#property(nonatomic, retain) AVCaptureStillImageOutput<feff><feff><feff><feff><feff> *stillImageOutput;
(U+FEFF is a Unicode Byteorder marker, and it looks like a normal space in Xcode, even if "Show Invisibles" is activated).
Deleting and re-inserting the space fixes the problem.
Go to the Editor menu, select Show Invisibles. Note that you have plenty of invisible, non printable characters between the AVCaptureStillImageOutput and the *. Delete everything between these two and finally put there a single space again. Those invisible characters, even though not printable, were taken as part of your class name. Once you are done, you can select Hide Invisibles again. Be careful when copy/pasting code, you even copy/pasted those "bad characters" into your SO question above.

Unknown Type Name in Xcode (even with #class declaration)

I'm having trouble with a simple app setting up a data controller. I get an error on the line #property (strong, nonatomic) BirdsListDataController *dataController; in BirdsListViewController.h. I've tried my best to use a #class declaration of BirdsListDataController, as well as trying to remove any #import statements from the .h files and tried to remove a circular #import which you can find commented out in the top of BirdsListViewController.h. I'm guessing it's something simple.
BirdsListViewController.h
#import <UIKit/UIKit.h>
#class BirdsListDataController;
#interface BirdsListViewController : UITableViewController <UITextFieldDelegate>
{
// NSMutableArray *listOfBirds;
IBOutlet UITextField *addNewBirdTextField;
}
//#property (nonatomic, retain) NSIndexPath *checkedIndexPath;
#property (nonatomic, retain) NSString *textLabelContents;
#property (nonatomic, retain) NSMutableArray *workingArray;
#property (strong, nonatomic) BirdsListDataController *dataController;
#property (strong, nonatomic) IBOutlet UITableView *birdListTableView;
#end
BirdsListViewController.m
#import "BirdsListViewController.h"
#import "BirdsListDataController.h"
#interface BirdsListViewController ()
#end
#implementation BirdsListViewController
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
...
BirdsListDataController.h
#import <Foundation/Foundation.h>
#class BirdName;
#interface BirdsListDataController : NSObject
#property (nonatomic, copy) NSMutableArray *listOfBirds;
-(NSUInteger)countOfList;
-(BirdName *)objectInListAtIndex:(NSUInteger)theIndex;
-(void)addBirdNameWithName:(BirdName *)bName;
#end
BirdsListDataController.m
#import "BirdsListDataController.h"
//#import "BirdsListViewController.h"
#import "Bird.h"
#implementation BirdsListDataController
-(id)init
{...
I'm still really new to iOS and Objective C, so hopefully my code isn't too awful to troubleshoot. Thanks for the help.
For people looking for a better answer than comment/uncomment your code, a better solution is to clean your project and to delete your derived data. Once you've fixed your circular references, the keystroke Command+Shift+K will clean your project, or you can go to and select Product->Clean.
To delete your derived data, open Organizer, click on the Projects tab, navigate to your project in the sidebar. You should see "Derived Data" under the project name header. To the right of that should be a button saying delete. If it is enabled, deleting the derived data can also remove hanging errors.
As way of explanation, it seems sometimes that Xcode becomes out of sync with a project, holding on to errors that no longer exists. This is better in more recent version, but still happens occasionally.
I'm not certain what is causing your problem, but a few things:
In the code that you've presented there is no reason not to import BirdListDataController.h in BirdListViewController.h, since there is no reference to BirdListViewControllers in BirdListDataController.h. So try replacing your #class declaration with an #import statement.
In BirdListDataController.h you declare #class BirdName, but in BirdListDataController.m you import Bird.h instead of BirdName.h. It seems like something could be wrong there, although I would have to see the code for BirdName.h and Bird.h to know for sure.
In my case I had duplicate class names in different folders structure, Once I removed the new class and named it differently everything worked again.
So to translate this into a practical solution, as per "shA.t"'s comment:
if you comment/uncomment your code, or clean project as above
answers suggested but still doesnt solve it:
take a step to look back at recent classes changes and double check
all class names are unique even if in different directories, double
check
them all
if duplicate class name found: make a backup of that
code, delete that class (not just reference, but to trash too)
create a new class with unique name and incorporate the backed up
code
For this particular duplicate class name scenario, this will save you the hassle of importing and commenting your #import "class.h"

Why iOS cannot get subviews property for class UIView using class_getProperty function?

I want to get the information about subviews property of class UIView:
objc_property_t property = class_getProperty([UIView class], "subviews");
But, it returns nil? I think it is so strange. Could someone explain this behavior to me?
Weird. If you use -valueForKey:, it can clearly be shown to exist. This used to be a bug with the old LLVM clang compiler in Xcode 3.2.3, where properties in categories (yes, it is declared in a category on UIView) wouldn't get recognized by the runtime, and there was even a bug report filed here about it. I know recent versions of Xcode have been having trouble with categories of late...
I just write a test code:
#interface Cat : NSObject
#property (nonatomic, strong) NSString *name;
#property (nonatomic) NSInteger age;
#property (nonatomic, readonly, copy) NSArray *subviews;
#end
It is ok. So Apple maybe be do so magic on it I guess.

Cocoa / Objective-C: Access a (Boolean) variable in different Classes

Situation:
Noob / Xcode 3.1
I have an AppView (NSView subclass) and an AppController (NSObject subclass)
in AppView.h i declare a boolean (BOOL: booleanDraw), which i set to 'NO' in AppView.m
When a button is clicked it 'launches' an action (AppController .h/.m) now i want to change booleanDraw to YES when the button is clicked.
I searched and found: do it with #property okay i tried to do that but it didnt work. (because i didnt totally get what to do probably)
i did:
#property BOOL booleanDraw;
(in AppView.h)
#implementation AppView
#synthesize(readwrite, nonatomic) booleanDraw;
(in AppView.m)
AppView *obj;
obj.booleanDraw = YES; // implicitly calls [obj setVar:3]
(in AppController.m)
Thanks for any help, i read some tutorials already but often they suggest some steps that should be basic but that dont belong to my repertoire, and the ADN often confuse me xD sorry but believe me im trying^^
You just reversed the synthesize and property statements:
in .h:
#property (nonatomic) booleanDraw;
(by default properties are readwrite, you only need to state when they are readonly)
in .m:
#synthesize booleanDraw;
In the controller you need to get the app view reference, the code you posted would not work unless you set "obj" to something.