I am a beginner in objective C and I am using the following code
I am using xcode6
// Car.h
#import <Foundation/Foundation.h>
#interface Car : NSObject {
NSString *simple_name; //This is private
#property int mssp; //Cant use #property Error illegal visibility specification
}
-(void) sayHi:(NSString*)msg ;
#end
Any suggestions on why I am getting this error ?
Move #property int mssp; out of the brackets:
// Car.h
#import <Foundation/Foundation.h>
#interface Car : NSObject {
NSString *simple_name; //This is private
}
#property int mssp;
-(void) sayHi:(NSString*)msg ;
#end
Related
I've found numerous posts about this error being able to resolve by adding the prefix of the project name to the class in entity viewer e.g ProjectName.Journey also the suggestion of adding #objc(ClassName) above the class declaration. But these solutions don't work for me. The problem is I have a NSManagedObject class generated in Objective C that I then try to access in Swift. I get the following error:
Unable to load class named 'ProjectName.Journey' for entity 'Journey'. Class not found, using default NSManagedObject instead.
Could not cast value of type 'NSManagedObject_Journey_' (0x1700541f0) to 'Journey' (0x100096f38).
My Class:
H:
#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>
#interface Journey : NSManagedObject
#property (nonatomic, retain) NSString * travelMode;
#property (nonatomic, retain) NSDate * startTime;
#property (nonatomic, retain) NSDate * endTime;
#property (nonatomic, retain) NSNumber * averageSpeed;
#property (nonatomic, retain) NSNumber * distance;
#end
M:
#import "Journey.h"
#implementation Journey
#dynamic travelMode;
#dynamic startTime;
#dynamic endTime;
#dynamic averageSpeed;
#dynamic distance;
#end
My code trying to access this code in Swift:
for res in results {
var move: Journey = res as! Journey; //error thrown here
var morningStamp = midnightDate!.timeIntervalSince1970;
var eveningStamp = morningStamp + 86400;
if(move.startTime.timeIntervalSince1970 > morningStamp) {
if(move.endTime.timeIntervalSince1970 < eveningStamp) {
filtered.addObject(move);
}
}
}
bridge header file:
//
// Use this file to import your target's public headers that you would like to expose to Swift.
//
#import "TravelRecordManager.h"
#import "Journey.h"
#implementation Fruit{
-(void) setWeight: (int)a{
weight=a;
}
-(void) setType:t{
Type=t;
}
-(void) setName:n{
name=n;
}
the error is in the 2nd line shown. i tried the show invisible spaces trick and it didnt work.
You've got an open bracket { next to your implementation, delete that and make sure your file ends in #end
Edit: The other problems are
You're writing your setters incorrectly. You need to provide a type like you did for setWeight int, for the type and name.
If you're going to make your own setters it needs to be _type = t, and _name = n
I just wrote this code and it builds without issue:
#interface Fruit : NSObject
#property (nonatomic) int weight;
#property (nonatomic, strong) NSString *type;
#property (nonatomic, strong) NSString *name;
#end
#import "Fruit.h"
#implementation Fruit
-(void) setWeight: (int)a{
_weight=a;
}
-(void) setType:(NSString *)t{
_type=t;
}
-(void) setName:(NSString *)n{
_name=n;
}
#end
You don't need brackets around your #implementation. Instead, you just need to put a #end after it
#implementation Fruit
...
#end
this is my h file:
#import <Foundation/Foundation.h>
#interface Base : NSObject
#end
this is my m file:
#import "Base.h"
#interface Base()
#property (nonatomic) int number;
#end
#implementation Base
-(void) setNumber: (int) p_number
{
self.number = p_number;
}
#end
this is what i want to accomplish
an h file:
#import <Foundation/Foundation.h>
#import "Base.h"
#interface Derived : Base
#end
and to do this in the m file
#import "Derived.h"
#implementation Derived
- (void) foo
{
self.number= 7;
}
#end
this of course results in error, please answer with a code that allows me to call the base property without placing the definition in the h file
thanks
Asking people to answer only with code is not a good idea. Why not ask for an explanation of what's wrong, so that you can fix this error and tons of similar errors in the future yourself?
The problem here is that you don't declare your property in the base header. If you don't want to do that, you can declare it in your subclass' implementation file as an extension of your base class:
Derived.m
#import "Derived.h"
#interface Base ()
#property (nonatomic) int number;
#end
#implementation Derived
- (void) foo
{
self.number= 7;
}
#end
You could use the same approach that Apple uses with UIGestureRecognizerSubclass.h
e.g. Create
// BaseSubclass.h
#interface Base (ForSubclassEyesOnly)
#property (nonatomic, assign) NSInteger number;
#end
You'll need to silence the compiler with
#implementation Base (ForSubclassEyesOnly)
#dynamic number;
#end
Now any class that want's to use these properties/call methods just includes this header.
e.g
#import "Derived.h"
#import "BaseSubclass.h"
#implementation Derived
- (void) foo
{
self.number = 7;
}
#end
In WebServiceAPI.h, which I referred in the code below, i declared a protocol with a required metod -(void) apiFinished:(WebServiceAPI *)api. When compiling the code i get this error: WebServiceAPI.h:13: error: expected ')' before 'WebServiceAPI' (line 13 is where the method of the protocol is declared). where am I doing wrong?
#import <Foundation/Foundation.h>
#protocol WebServiceAPIDelegate
#required
-(void) apiFinished:(WebServiceAPI *)api;
#end
#interface WebServiceAPI : NSObject{
NSString *address;
NSMutableData *dataWebService;
}
#property (nonatomic, assign) id <WebServiceAPIDelegate>delegate;
#property(nonatomic, retain) NSString *address;
#property(nonatomic, retain) NSMutableData *dataWebService;
#end
The problem is that WebServiceAPIDelegate doesn't know about the class WebServiceAPI when it is defined. Add a #class directive before you create WebServiceAPIDelegate #protocol declaration.
// Add the following line to let the compiler stop worrying about
// the existance of class WebServiceAPI
#class WebServiceAPI;
#protocol WebServiceAPIDelegate
#required
-(void) apiFinished:(WebServiceAPI *)api;
#end
I am new to iphone development. I have this code in the delegate.h section:
#import <UIKit/UIKit.h>
#import <objc/Object.h>
#class Learning1ViewController;
#interface Greeter: NSObject<UIApplicationDelegate>
{
}
-(void)greet;
#end
#include <stdio.h>
#implementation Greeter
-(void) greet
{
printf ("Hello, World!\n");
}
#include <stdlib.h>
int main(void)
{
id myGreeter;
myGreeter=[Greeter new];
[myGreeter greet];
[myGreeter free];
return EXIT_SUCCESS;
}
#end
#interface Learning1AppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
Learning1ViewController *viewController;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet Learning1ViewController *viewController;
#end
When i compile i get this error:
ld: duplicate symbol _main in /Users/ianbennett/Desktop/iphone development/Learning1/build/Learning1.build/Debug-iphonesimulator/Learning1.build/Objects-normal/i386/Learning1AppDelegate.o and /Users/ianbennett/Desktop/iphone development/Learning1/build/Learning1.build/Debug-iphonesimulator/Learning1.build/Objects-normal/i386/main.o
command/Developer/platforms/iphoneSimulator.platform/Developer/usr/bin/gcc-4.2 failed with exit code 1
I have seen that other people have had similar errors and that it might be to do with my library but im not really sure how to fix it.
You have defined the main() function twice (looks like it's defined in Learning1AppDelegate.m and main.m
Thanks for the error message
You have defined the same function main in 2 places
The files are
main
Learning1AppDelegate
You can only define a function in one place - so you have to choose