XCode Throwing Errors #implementation - objective-c

I'm trying to learn some of the basics of developing OS X apps with XCode and Objective-C, but I am already running into problems.
I have a project I made from a while back which worked very well for me, however, when I try to replicate the results I had last time, I run into numerous errors.
I have two files, a .c and a .h named "AppDelegate"
in AppDelegate.h:
#import <Cocoa/Cocoa.h>
#import <WebKit/WebView.h>
#interface AppDelegate : NSObject {
IBOutlet WebView *gameFrame;
}
#end
then, in AppDelegate.c:
#import "AppDelegate.h"
#implementation AppDelegate
-(void)awakeFromNib
{
}
#end
In IB, there is an NSObject named 'AppDelegate' and its class is 'AppDelegate'.
However, when I try to run this, I get 11734 errors...
When I click on the error icon at the bottom of the XCode window, it lists a bunch of code that seems to be involving NSStrings, but I cant make any sense of it...
Also, within my code, the
#end
line in both the .c and the .h are highlighted with an error saying:
'Expected identifier or '(' before '#' token.'
I don't understand what XCode is tripping up on when it tries to compile, I don't see any logical place for a '(' to go and I don't think I left anything unidentified.
Any help would be appreciated.

That's because that isn't valid C code.
You named your module file AppDelegate.c, which indicates that it contains source code written in (more or less) pure C. But it does not: You wrote a class interface and implementation in Objective-C, which is a superset of C (all C is valid Objective-C, but not all Objective-C is valid C—in particular, classes aren't).
For this, you must name the module file AppDelegate.m (or anything else, as long as it ends with .m; naming it after the class is a convention worth following). The .m suffix indicates a module (usually containing a class implementation) written in Objective-C.
So, just rename your module file from AppDelegate.c to AppDelegate.m. Make sure you do this in Xcode, not the Finder: If you do it in the Finder, Xcode will only care that there is no longer a file named AppDelegate.c; it won't notice the rename.
For your convenience in creating future classes, Xcode provides a template in the “New File” panel for creating subclasses of certain Cocoa classes; your AppDelegate should be a subclass of NSObject, and templates are also provided for NSView, NSDocument, UIView, UIViewController, and a few others. The files created by the template will already have the correct extensions.

Related

Are #interface and #end always necessary in .h file?

I noticed a strange thing in sample code downloaded from apple dev center. the sample code is Footprint: Indoor Positioning with Core Location. There is no #interface and #end in AAPLMKMapRecRotated.h file. But it works well in simulator. Can anyone explain why? Thanks!
"#interface" and "#end" are necessary wherever you want to declare the interface to an Objective-C class. If you don't declare the interface of an Objective-C class in your header, it's obviously not needed. If you declare interfaces of three Objective-C classes in your header, you'll need it three times. If you declare the interface of an Objective-C class in a .m file, so nobody outside that file can see it, then you put it into the .m file.
#interface ... #end declare a ObjC class.. Not all .h file nor all Apple API rely on ObjC, a lot of C is used thorough their framework (CoreAudio, CoreMidi, CoreGraphic, GrandCentralDispatch...) in those framework .h file are classic C header used as such and therefore no #interface .. #end...

Cocoa ViewController.m vs. Cocoa Touch ViewController.m

Total newbie question follows...
I am learning Objective C based Cocoa (OS X) app development. Most of the books and videos I have are iOS based, so I'm seeing how the two platforms differ.
I have converted a simple iOS app to OS X. I got the converted OS X app to work fine, but I have a question: Why doesn't OS X ViewController.m file have an #interface section?
I created the iOS app using Interface Builder, dragging UI elements from Main.Storyboard to ViewController.m, in the assistant editor. I dragged a UILabel cell into the #interface section of ViewController.m, created an instance variable #property. In iOS, the ViewController.h public #interface section remains empty.
In the OS X app, I dragged an NSTextField cell to the #interface section of the ViewController.h file to create a similar instance variable.
Is the OS X ViewController.h #interface section the correct place for IBOutlets? Doesn't this expose these instance variables to other classes? In the iOS case, since the instance variables were NOT added to the ViewController.h file, I assume they are hidden, correct? Can I make the OS X instance variables private?
As I mentioned, the OS X code works, I'm just trying to understand the differences between the platforms, and make sure I am headed in the right direction.
Before I answered I wanted to check if the same happened to me-- but when I created a custom NSViewController in a new iOS OR OS X app, both would generate the #interface in the implementation file (.m file)
So I'm not sure why you are seeing that.
However, to answer your question, (as you said) properties declared in the #interface in your header file (.h) are public. While properties declared in the #interface in the implementation are private. This is because the #interface inside of your implementation is called an "Extension". Which is basically just an anonymous "Category".
Unless outside objects need a reference to your outlets, then I would continue placing them inside of your implementation file. If for whatever reason one isn't generated for you, there is nothing wrong with manually typing out your own class extension to declare the properties
Yes you are right instance variable should not added to ViewController.h file to make them private. Same like you can make instance variables as private variables in OS X . Like this`#interface MasterViewController ()
#property (nonatomic, weak) NSString* strTemp;
#end
#implementation MasterViewController
You can get difference between cocoa and cocoa touch by already posted question for this

Objective-C: Instance variables may not be placed in categories

I am learning Objective-C and the tutorials I am watching are using an outdated version of Xcode. So, I am not sure whether I created a wrong type of class or if there is something wrong with the code.
In Xcode I Did Command+N do create a new file, clicked 'Source' under 'OS X' and clicked Objective-C File. Then I for file type I chose Category and class: NSObject. In the tutorial the guy made a subclass and since this was the only Objective-C related file in the new Xcode I decided to use this. After making the header and main files, I created the following text.
#import <Foundation/Foundation.h>
#interface NSObject (Person){
NSString *myName;
NSNumber *myAge;
NSNumber *myGrade;
}
#end
Thanks alot, Coler234
A category is an extension of an existing class with some new methods. To indicate you are defining a category rather than a new class, follow the class name with the category name in parentheses as you did in your code(Person). What you actually wanted, though, is a subclass of NSObject called Person, so you need to say
#interface Person : NSObject

how to access a newly created class in objective c

I get the feeling that this is a very stupid question, but I cannot find an answer anywhere. I've created a new class in an XCode project called "Word", with its own .h and .m files, which inherits from NSString, and has its own declared instance variable, method, etc. How do I access it within the viewcontroller.h and .m files? I though it would just show up in the little suggestion box in XCode, like the pre-made classe, but it doesn't. How do I use my new class?
#import "Word.h"
I will note that subclassing NSString is extremely uncommon and almost never what you mean to do. You probably meant to have Word have an NSString property.
in your .h, at the top type #import " then start typing Word.h. It should autocomplete if the .h and .m files are in your project correctly.
You just need to add the following to your ViewController.h
#import "Word.h"

Objective C 'target action' in .m and 'outlet' in .h

I'm currently taking the Stanford CS193p iPhone and iPad application development course ( http://itunes.apple.com/itunes-u/ipad-iphone-application-development/id473757255 ) and in the second lecture I got stumped when he connected the buttons of the calculator to the .m file, he connected the display label (that shows the value on the calculator) to the .h file...
My understanding of the whole implementation and header file thing was that you declare the class members in header and say what they do in the implementation, which is why this doesn't make sense to me...
any help would be greatly appreciated
It is possible in Objective-C for a .m file to contain methods for a class that the .h file for the class doesn't declare. In fact with the Categories feature of the language there can be more than one .h and .m that define all the methods for a class.
As far as what can be connected, the keyword IBOutlet marks properties or variables and IBAction marks action methods. These are just hints for the editor; all of the "connections" are set up at runtime automatically.