NSImage forward declaration error - objective-c

I'm trying to convert a NSURL to a NSImage but when I try to do so with the following code:
NSURL *URL = [NSURL URLWithString:[myArray objectAtIndex:i]];
NSImage *myImage = [[NSImage alloc] initWithContentsOfURL:URL];
xCode gives me the error "Receiver type 'NSImage' for instance message is a forward declaration. According to other SO posts, it's a problem with importing. However, I'm already importing my .h file as well as CoreData and I've tried using this:
#ifdef __OBJC__
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>
#endif
from Receiver type is forward declaration. I have no idea what to do at this point, I'm sure it's obvious but I cannot figure it out. My header for my .m file is
#import "myClass.h"
#import <Foundation/Foundation.h>
and CoreData is being imported in the .h file.
Thank you!

For Mac, you want AppKit rather than UIKit.

Related

Must AVAudioPlayer be instantiated from a ViewController class?

I'm hoping someone can help as I'm new to iOS / objective C and very puzzled. I'm trying to play a simple sound using AVAudioPlayer as follows:
NSString *path = [[NSBundle mainBundle] pathForResource:#"soundFile" ofType:#"wav"];
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath: path];
self.player=[[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:NULL];
[self.player play];
I am using ARC so I also have in my .h file, the following reference to my player so that ARC does not deallocate my player prematurely:
#property (nonatomic, strong) AVAudioPlayer *player;
This code works just fine and plays my sound PROVIDED that I run this code from a ViewController or my application's AppDelegate.
However if I cut and paste this very same code, plus all the necessary #includes and the #property and add them into another class in the same application but which is not a ViewController, and call the code there then no error is raised but no sound is played.
It is exactly the same code just called on a different class??
Why would it not work?
I have looked and looked for a similar post but nowhere have I seem exactly this scenario addressed. Many thanks if you can help me- would be much appreciated.
To clarify the issue-- here is how I call this code on another class say a class I have named Audio Tester, I would write in AppDelate say
#import "AppDelegate.h"
#import "AudioTester.h"
#interface AppDelegate ()
#end
#implementation AppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
AudioTester * tester = [[AudioTester alloc]init];
[tester playAudio];
}
where AudioTester playAudio is defined as
#import "AudioTester.h"
#implementation AudioTester
-(void) playAudio {
NSString *path = [[NSBundle mainBundle] pathForResource:#"soundFile" ofType:#"wav"];
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath: path];
self.player=[[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:NULL];
[self.player play];
}
#end
with AudioTester.h as follows
#import <Cocoa/Cocoa.h>
#import <Foundation/Foundation.h>
#import <AVFoundation/AVFoundation.h>
#interface AudioTester : NSObject
#property (nonatomic, strong) AVAudioPlayer *player;
-(void) playAudio;
#end
Stepping through this code, it gets called just fine but it does not play sound?
If you can help that would be much appreciated. I'm totally stumped.
a little conceptual explanation about your code:
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
AudioTester * tester = [[AudioTester alloc]init];
[tester playAudio];
}
1.
if you use ARC then the instance won't be not kept alive after the scope runs out, therefore the tester object will be immediately released, so in your case the object is deallocated before it could do anything – that is the reason why you can't hear any noise or sound.
if you want to keep your tester instance alive independently from your current scope where you inited in, you need to create like e.g. a property which is outside of the scope; you could put that into a class extension for instance:
#interface AppDelegate ()
// ...
#property (nonatomic, strong, nullable) AVAudioPlayer * tester;
// ...
#end
2.
we don't put anything like this into the AppDelegate.m file, the app delegate class basically handles the app-related events globally like launch, terminate, etc... briefly, the global and major events of the app's life cycle in runtime.
you can read more about its purpose in the official docs.
3.
you may use the –applicationDidFinishLaunching: method deliberately for initing your app, but I feel necessary to mention you may want to put everything inside the method –application:didFinishLaunchingWithOptions: instead.
you can read more about the initial procedure as well in the same documentation.
TL;DR
the answer to your original concern: NO, a class can be inited and instantiated in any other instance of any type of classes in general, but you need to worry about keeping the object alive as long as you want to use it.

Storing an iOS Application Constant in Objective-C

I have a few unicode characters (musical flat and sharp symbols) that I currently have defined in a class:
#import <Foundation/Foundation.h>
#define kSongsSharpSymbol [NSString stringWithFormat:#"\U0000266F"]
#define kSongsFlatSymbol [NSString stringWithFormat:#"\U0000266D"]
#interface Song : NSObject {
//...
}
As the application grows I think these types of constants would be better off placed someplace that is available to the application files without having to include the class.
Questions
Am I defining the unicode characters correctly? It works but that doesn't mean it's right
Where, ideally, should I be placing these types of constants?
Cheers and Thanks!
I solved this by creating a custom class like so, and then adding it to my precompiled header file:
//Constants.h
#import <Foundation/Foundation.h>
FOUNDATION_EXPORT NSString *const kSongsSharpSymbol;
FOUNDATION_EXPORT NSString *const kSongsFlatSymbol;
//Constants.m
#import "Constants.h"
NSString *const kSongsSharpSymbol = #"\U0000266F";
NSString *const kSongsFlatSymbol = #"\U0000266D";
//.pch file
#import <Availability.h>
#ifndef __IPHONE_4_0
#warning "This project uses features only available in iOS SDK 4.0 and later."
#endif
#ifdef __OBJC__
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import "Constants.h"
#endif

iOS: What can cause the Xcode compiler to throw errors when I include my AppDelegate.h into another header file?

I am trying to #import my "AppDelegate.h" into another header file of the same project in order to access methods of the AppDelegate of my iOS project.
So, my header file looks something like this:
#import
#import "DataProvider.h"
#import "MyAppDelegate.h"
#interface MyViewController : UIViewController <UITextFieldDelegate, UIAlertViewDelegate> {
...
}
and I want to use my AppDelegate like this:
MyAppDelegate* appDelegate = (MyAppDelegate*)[UIApplication sharedApplication].delegate;
[appDelegate doSomething];
However, as soon as I #import "MyAppDelegate.h", the compiler throws a lot of (unrelated) errors, like
Cannot find interface declaration for 'MyOtherViewController', superclass of 'MyOtherViewController2'
The thing is: I can include my AppDelegate just fine in other headers. And I cannot figure out what the difference might be. Please help me figure out what could cause this! Thanks a lot!
PS: This happens with GCC as well as the new LLVM.
Move the #import into the .m file. If you need the MyAppDelegate symbol in your .h, use #class MyAppDelegate; instead.

Why can you only #define an NSString constant in a header file?

A curious objective-c newbie question. I noticed that I can #define an NSString in the .h file, but not in the .m file... why?
The declaration in question is:
#define kSomeString #"This is a string"
That declaration fails with an error if its in the .m file.
you CAN define them also in the implementation files.
#import "SAiPadHomeViewController.h"
#define hugo #"Test"
#interface SAiPadHomeViewController ()
#end
#implementation SAiPadHomeViewController
#end
This example works - try it.
Greetz!

Error coming up when trying to implement Google map directions, method definition not found

I have been trying to add a link to Google Maps so that I can show some directions to a location. The only problem i'm having is the code not recognizing the method. Here's samples below, hope they help.
.h:
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
#interface More : UIViewController{
}
- (IBAction)directions:(id)sender;
-(CLLocationCoordinate2D)getCurrentLocation;
#end
.m:
- (IBAction)directions:(id)sender {
CLLocationCoordinate2D currentLocation = [self getCurrentLocation];
NSString* address = #"********";
NSString* url = [NSString stringWithFormat: #"http://maps.google.com/maps?saddr=%f,%f&daddr=%#",
currentLocation.latitude, currentLocation.longitude,
[address stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
[[UIApplication sharedApplication] openURL: [NSURL URLWithString:url]];
}
Error:
Method definition for 'getCurrentLocation not found
Thanks
It looks like you haven't written getCurrentLocation. I'm not sure what that method is but google for CoreLocationControllerDelegate, that will show you how to get your location.
Is there a reason you don't want to use MKMapView? It really makes your life easy.