I'm trying to implement a database system where an entity contains some methods in its DataClass.m but I cannot call them in my ViewController.m
In the code above I try to call savePersona but the build says
No known class method for selector
'savePersona:etaLabel:indirizzoLabel:contextLabel:'
ViewController.m
#import "ViewController.h"
#import "Giovanni+CoreDataClass.h"
#import "AppDelegate.h"
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
/*
* Event after clicking button Save.
*/
- (IBAction)btnSave:(id)sender {
NSString *nome = _insertName.text;
NSString *eta = _insertAge.text;
NSString *indirizzo = _insertIndirizzo.text;
// Check for all inputs
if ([nome isEqualToString:#""] || [eta isEqualToString:#""] || [eta intValue] <= 0 || [indirizzo isEqualToString:#""] ) {
_resultText.text = #"All fields are required";
return;
}
[Giovanni savePersona:nome etaLabel:[eta intValue] indirizzoLabel:indirizzo contextLabel:[((AppDelegate *)[[UIApplication sharedApplication] delegate]) managedObjectContext]];
}
Giovanni+CoreDataClass.h
#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>
#interface Giovanni : NSManagedObject
+(void)savePersona: (NSString*)nome etaLabel:(int16_t)eta indirizzoLabel:(NSString*)indirizzo contextLabel:(NSManagedObjectContext *)context;
#end
#import "Giovanni+CoreDataProperties.h"
Giovanni+CoreDataClass.m
#import "Giovanni+CoreDataClass.h"
#implementation Giovanni
+(void)savePersona: (NSString*)nome etaLabel:(int16_t)eta indirizzoLabel:(NSString*)indirizzo contextLabel:(NSManagedObjectContext *)context {
Giovanni *p1 = [[Giovanni alloc] initWithContext:context];
p1.nome = nome;
p1.eta = eta;
p1.indirizzo = indirizzo;
}
In Build Phases I have Giovanni+CoreDataClass.m and Giovanni+CoreDataProperties.m.
In Copy Bundle Recources I have DatabaseExample.xcdatamodeld (DatabaseExample is the name of the project)
Looks like you have named your class file as category, but you are not using categories.
Try to rename Giovanni+CoreDataClass to something like GiovanniCoreData.
I have resolved my problem by recreating the project.
Nothing has changed but it seams that XCode encounter some problems on autocreating my entities when i click on Editor -> Create NSManagedObject Subclasses...
I don't know if it's a bug of the latest versione but now the project works fine.
Related
I have a DataHandler class which acts like a singleton, and it has sharedHandler object method. I use it throughout the whole project, but for some reason, I can't use it in AppDelegate.
DataHandler.h
#import <Foundation/Foundation.h>
#interface DataHandler : NSObject
+ (id)sharedHandler;
- (void)logout;
#end
DataHandler.m
#import "DataHandler.h"
/**
* Singleton static method
* #return singleton instance
*/
+ (id)sharedHandler {
static DataHandler *sharedHandler = nil;
#synchronized (self) {
if (sharedHandler == nil)
sharedHandler = [[self alloc] init];
}
return sharedHandler;
}
-(void) logout {
// ...
}
#end
AppDelegate.m
#import "AppDelegate.h"
#import "DataHandler.h"
#implementation AppDelegate {
- (void)applicationWillResignActive:(UIApplication *)application {
[[DataHandler sharedHandler] logout];
}
#end
I keep getting:
Error: no known class method for selector 'sharedHandler`
Error: no known instance method for selector 'logout'
What is the cause of this behavior?
You have two files named DataHandler.h and the import in AppDelegate.m is picking up the wrong file. Note that it may be picking up a file that's not actually in your project, as long as it's in the folder on disk.
//
// AppDelegate.m
//
#import "AppDelegate.h"
#import "MainViewController.h"
#import "SetupViewController.h"
#import <Cordova/CDVPlugin.h>
#import "NSObject+Utils.h"
#import "WebProjectSetupHelper.h"
#import "WebProjectSetupHelperDelegate.h"
#import "LoadingView.h"
#import "AppDelegate.h"
#import "MainViewController.h"
#import "NSObject+Utils.h"
#import "DeviceModelInfo.h"
#import "UIDevice+System.h"
#import "Alert.h"
#interface AppDelegate () <WebProjectSetupHelperDelegate>
#property (nonatomic, strong) WebProjectSetupHelper *helper;
#property (nonatomic, strong) LoadingView *loadingView;
#end
#implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[self createItemsWithIcons];
// determine whether we've launched from a shortcut item or not
UIApplicationShortcutItem *item = [launchOptions valueForKey:UIApplicationLaunchOptionsShortcutItemKey];
if (item) {
NSLog(#"We've launched from shortcut item: %#", item.localizedTitle);
} else {
NSLog(#"We've launched properly.");
}
return YES;
}
I am using this code in my App Delegate but on the line that contains "[self createItemsWithIcons];", I get an error that says "No visible #interface for 'AppDelegate' declares the selector 'createItemsWithIcons'".
Any idea what I am doing wrong? I am trying to add quick actions with 3D touch to my app.
Update! I got that issue resolved. Now my next question is from my AppDelegate, how can I run code that is in different file? In the "www" folder there is a file that ends in .js. In that file, there is javascript code that I want to run. Any Ideas on how to call that file?
You have missed to add implementation for createItemsWithIcons method.
https://github.com/versluis/3D-Touch/blob/master/3D%20Touch/AppDelegate.m
If you are working with 3D-Touch sample, then add this code to AppDelegate:
- (void)createItemsWithIcons {
// create some system icons (doesn't work)
// UIApplicationShortcutIcon *loveIcon = [UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeLove];
// UIApplicationShortcutIcon *mailIcon = [UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeMail];
// UIApplicationShortcutIcon *prohibitIcon = [UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeProhibit];
// icons with my own images
UIApplicationShortcutIcon *icon1 = [UIApplicationShortcutIcon iconWithTemplateImageName:#"iCon1"];
UIApplicationShortcutIcon *icon2 = [UIApplicationShortcutIcon iconWithTemplateImageName:#"iCon2"];
UIApplicationShortcutIcon *icon3 = [UIApplicationShortcutIcon iconWithTemplateImageName:#"iCon3"];
// create several (dynamic) shortcut items
UIMutableApplicationShortcutItem *item1 = [[UIMutableApplicationShortcutItem alloc]initWithType:#"com.test.dynamic" localizedTitle:#"Dynamic Shortcut" localizedSubtitle:#"available after first launch" icon:icon1 userInfo:nil];
UIMutableApplicationShortcutItem *item2 = [[UIMutableApplicationShortcutItem alloc]initWithType:#"com.test.deep1" localizedTitle:#"Deep Link 1" localizedSubtitle:#"Launch Nav Controller" icon:icon2 userInfo:nil];
UIMutableApplicationShortcutItem *item3 = [[UIMutableApplicationShortcutItem alloc]initWithType:#"com.test.deep2" localizedTitle:#"Deep Link 2" localizedSubtitle:#"Launch 2nd Level" icon:icon3 userInfo:nil];
// add all items to an array
NSArray *items = #[item1, item2, item3];
// add this array to the potentially existing static UIApplicationShortcutItems
NSArray *existingItems = [UIApplication sharedApplication].shortcutItems;
NSArray *updatedItems = [existingItems arrayByAddingObjectsFromArray:items];
[UIApplication sharedApplication].shortcutItems = updatedItems;
}
I am a complete beginner at Objective c and I am trying to complete a challenge in the book "iOS programming: The big Nerd Ranch guide".
I'm trying to put an object called item (of the class BNRItem) into an NSMutableArray called subItems which is part of an object called container (of the class BNRContainer, a subclass of BNRItem with the addition of the NSMutableArray to hold BNRItems). BNRItem works fine.
The code is as follows:
BNRContainer.h
#import <Foundation/Foundation.h>
#import "BNRItem.h"
#interface BNRContainer : BNRItem
{
NSMutableArray *subItems;
}
BNRContainer.m
- (id)init
{
return [self initWithItemName:#"Container"
valueInDollars:0
serialNumber:#""];
}
- (void)setSubItems:(BNRItem*)item
{
[subItems addObject:item];
}
Main.m
#import <Foundation/Foundation.h>
#import "BNRItem.h"
#import "BNRContainer.h"
int main(int argc, const char * argv[])
{
#autoreleasepool {
BNRItem *item = [[BNRItem alloc] init];
BNRContainer *container = [[BNRContainer alloc] init];
[container setSubItems:item]
}
return 0;
}
At the line [container setSubItems:item] I get the error: No visible #interface for container declares the selector setSubItems
The setter method setSubItems doesn't code complete (although other setters do, and work fine).
Am I doing something simple wrong? A simple explanation would be very appreciated!
Update BNRContainer.h:
#import <Foundation/Foundation.h>
#import "BNRItem.h"
#interface BNRContainer : BNRItem
{
NSMutableArray *subItems;
}
- (void)setSubItems:(BNRItem*)item;
#end
(Dunno why Fred deleted his answer.)
In order for Xcode to generate the getters/setters for subItems, you have to actually declare the property for it in your interface. Something like this:
#import <Foundation/Foundation.h>
#import "BNRItem.h"
#interface BNRContainer : BNRItem
#property (strong, nonatomic) NSMutableArray *subItems;
#end
Additionally, you aren't ever actually alloc/initing your array, and the current logic for setSubItems: will not do what it sounds like it would do. This function would add the array passed as a parameter as an object within SubItems. If you're trying to add items from an array to subitems, then you should be using:
[myMutableArray addObjectsFromArray:<#(NSArray *)#>];
I created a class called Datahandler with 2 methods. In my LoginViewController, I can use them, but in another ViewController they cannot be found.
This is my DataHandler.h file:
#import <Foundation/Foundation.h>
#interface DataHandler : NSObject
- (int) loginOnServer:(NSString *)username password:(NSString *)password;
- (NSString *) getJsonFromServer;
#end
In my LoginViewController, both methods are found.
LoginViewController.m:
#import "DataHandler.h"
...
#interface LoginViewController ()
...
#end
#implementation LoginViewController
- (void) checkLogin:(NSString *)email password:(NSString *) password
{
DataHandler *dataHandler = [[DataHandler alloc] init];
int result = [dataHandler loginOnServer:email password:password];
...
}
...
#end
FirstViewController.m:
#import "DataHandler.h"
...
#interface FirstViewController ()
#end
#implementation FirstViewController
- (void)viewDidLoad
{
...
DataHandler *dh = [[DataHandler alloc] init];
NSString *json = [dh getJsonFromServer];
...
}
What I get there is:
FirstViewController.m:32:23: No visible #interface for 'DataHandler' declares the selector 'getJsonFromServer'
What am I doing wrong?
Thank you.
Edit 1:
There was another old class called DataHandler in my project folder, which was used instead of the this one.
Please delete your or rename your another DataHandler file and give the reference in your buildPhases (Compile sources)
As you can see log is showing no visible interface that means definition of method is not available in your another DataHandler.h, therefore receiver isn't able to get the declaration of your method.
I want to call c++ class in my ViewController. So I create a class like this:
Hello.h
#import <Foundation/Foundation.h>
#interface Hello : NSObject{
class NewHello{
private:int greeting_text;
public:
NewHello(){
greeting_text=5;
}
void say_hello(){
printf("Greeting_Text = %d",greeting_text);
}
};
NewHello *hello;
}
-(void)sayHellooooo;
#end
Hello.mm
#import "Hello.h"
#implementation Hello
-(void)sayHellooooo{
hello = new NewHello();
hello->say_hello();
}
#end
ViewController.h
#import <UIKit/UIKit.h>
//#include "Hello.h"
#class Hello;
#interface ViewController : UIViewController{
}
#end
ViewController.m
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
NSLog(#"dddddddd");
Hello *aa = [[Hello alloc]init];
[aa sayHellooooo];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
#end
It works fine in the project:http://files.cnblogs.com/cpcpc/Objective-C%E8%B0%83%E7%94%A8C.rar
But when I copy the code to my project,it appears "Receiver type for instance messages is a forward declaration" error.
If I change "#class Hello;" to #import "Hello.h",it appears "Unkwon type class,did you mean Class" error in "class NewHello".
I use xcode 4.6.Can anybody help me?Thank you!
The problem is (as you've stated) file type for ViewController.m is Obj-C and Hello.h is a Obj-C++ file. The solution is to add
#import "Hello.h"
to your ViewController.m file and change the file type of ViewController.m to Obj-C++ (from the right panel)