Linking to Quartz framework or ARC error? - objective-c

I have the following code to generate a "noise texture" for an NSView background.
I have already linked the target to Cocoa and QuartzCore frameworks in the build phases, but the compiler keeps complaining with the following errors:
ARC semantic issue
"Receiver 'CIFilter' for class message is a forward declaration"
"Not known class method for selector filterWithName"
CIFilter *randomGenerator = [CIFilter filterWithName:#"CIColorMonochrome"];
[randomGenerator setValue:[[CIFilter filterWithName:#"CIRandomGenerator"] valueForKey:#"outputImage"]
forKey:#"inputImage"];
[randomGenerator setDefaults];
noisePattern = [randomGenerator valueForKey:#"outputImage"];
What's wrong here?.
I have already try all tricks that I can imagine off, to get rid of the errors. I know for sure this code works because it is a copy off an example project that compiles without any problems.
Any help is appreciated.

However you linked your project with QuartzCore framework you still need toimport appropriate header in your implementation file:
#import <QuartzCore/QuartzCore.h>

Related

How to import and use Swift Pod Framework in Objective-C Project

I've been trying to checkout CocoaPods new framework setup to get some Pods going and I'm having trouble using the Swift one's in my Objective-C project.
First things first, this is CocoaPods prerelease 0.35, you can read about how to use and install it here.
Here's my current Podfile:
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '8.0'
pod 'MBProgressHUD'
pod 'SLPagingViewSwift'
MBProgressHUD is a common spinning indicator, and SLPagingViewSwift is a random project I found by typing Swift into the cocoapods search. Here's the ViewController.m In my project:
#import "ViewController.h"
#import SLPagingViewSwift;
#import MBProgressHUD;
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
// Works just fine
MBProgressHUD *hud = [[MBProgressHUD alloc] initWithView:self.view];
[self.view addSubview:hud];
[hud show:YES];
// Causes Error -- Won't build
SLPagingViewSwift *sl = [[SLPagingViewSwift alloc] init];
}
#end
Here's the SLPagingViewSwift declaration:
class SLPagingViewSwift: UIViewController, UIScrollViewDelegate {
As you can see, it inherits from UIViewController, so it shouldn't be a problem to just allocate it and initialize it. If I add the file separately as just a file, the above code runs just fine. I know it works.
tl;dr
How can I use a pure Swift Framework created by CocoaPods in a pure Objective-C class?
TroubleShooting
Mostly I've been trying various imports. Apple recommends the #import style here
But I have been trying multiple other varieties:
// Compiler Error
#import <SLPagingViewSwift/SLPagingViewSwift.h>
// Builds Fine -- Doesn't Work
#import <SLPagingViewSwift/SLPagingViewSwift-Swift.h>
#import "SLPagingViewSwift-Swift.h"
I've also been trying a few other Swift libraries from time to time to see if I could make anything click.
I don't see anything on the Cocoapods issues that can help this, I also didn't find anything in their blog / release stuff.
Note
If I add the SLPagingViewSwift.swift file separately to the project the old fashioned way, it works just fine.
I think you have to declare the swift class as public, otherwise it is treated as an internal class and can be only be seen within the same module, and this could be the reason why adding it to the same project as files work, but as a framework doesn't. Other thing that occurs to me is that the framework may need to add #objc in front of the class declaration so that it can be seen within objective-c classes. Also reading Apple's guide of Mix and Match between objective c and swift it says that when you import an external framework, you need to make sure the Defines Module build setting for the framework you’re importing is set to Yes. Have you checked with any of those options?
Jus use the
#import SwiftModuleName;
Syntax, and make sure the functions you want to use are public (and #objc)
In my case there was no “use_frameworks!” into podfile (old project).
I added it and then I was able to use import like that
#import "PODNAME-Swift.h"
and use classes from pod.
But finally I wasn't able to use that swift pod, because of lack objective c exposition. I believe this will be the issue in many cases.

Compiler error after upgrading to Xcode 4.6 using setDataSource

I was upgrading some projects to use Xcode 4.6 and Clang LLVM compiler but I'm getting stuck with an error. I'm not the biggest obj-c geek so I'm a bit stuck here. I think this question touches base with my problem but doesn't quite give me a solution.
My class derives from NSTableView (#interface MyClass : NSTableView) but when I make the following call:
[self setDataSource:self];
I get the following error:
Cannot initialize a parameter of type 'id<NSTableViewDataSource>' with an lvalue of type 'MyClass *'
Using Xcode 3.2.6 had no issues with this call but now I'm stumped as to why I'm getting an error. I do add some table columns previous to this call and set some attributes but essentially I get the error from the call above.
Any help is appreciated.
For one, your table view should NOT be its own datasource. This breaks the MVC model that Cocoa uses. Views should only be concerned about displaying/presenting data, it should not store the data (that's the model's job) nor should it be the class that interfaces the view with the model (that's the controller's job).
Usually the datasource will be a NSViewController that conforms to the NSTableViewDataSource and the protocol:
#interface MyViewController : NSTableViewController <NSTableViewDataSource, NSTableViewDelegate>

Property anchorPoint cannot be found in forwar class object CALayer

I downloaded this sample: https://developer.apple.com/library/ios/#samplecode/Touches/Introduction/Intro.html#//apple_ref/doc/uid/DTS40007435
If I start the sample app everything is working fine.
I made my own app and copied most of the code into it. There is a method called adjustAnchorPointForGestureRecognizer: But in this method there is a error but only in my app (see screenshot): "Property anchorPoint cannot be found in forwar class object CALayer". I am wondering how I can solve this issue, I don't know what I did wrong. It is exactly the same code from the sample. Anyone can help? Thanks!
Did you import the QuartzCore framework into your project? Maybe that's why it can find the CALayer class
This error is thrown by XCode when it can't find a definition for CALayer. You should add the framework to your project and add #import <QuartzCore/QuartzCore.h> to wherever you are using the class

Creating a FBProfilePictureView in Storyboard

I created a UIView inside the main view of a view controller using the storyboard editor and changed its class to FBProfilePictureView.
I created an outlet:
#property (weak, nonatomic) IBOutlet FBProfilePictureView *userImage;
However, when I refer to the userImage object in code it reports itself as a UIView.
NSLog(#"userImage class: %#", [userImage class]);
Produces:
2012-08-28 17:52:22.196 FBTestApp[6230:707] userImage class: UIView
What am I missing?
While I didn't see the error mentioned in the FB docs, adding:
[FBProfilePictureView class];
To applicationDidFinishLaunching did the trick. Presumably some runtime magic going on.
I wouldn't rely on [userImage class] for anything other than calling class methods. If you need to ensure userImage is the correct type, use [userImage isKindOfClass:[FBProfilePictureView class]]. It will let you know if you can treat the object as a FBProfilePictureView.
A more elegant way to resolve this might be adding the -ObjC linker flag instead of doing this "runtime magic" stuff. Here you can find instructions on how to add the flag!
See the SDK documentation, which says:
Note: If you've added the -ObjC flag to your linker options, then you don't have to add this code. Adding that flag causes the linker to load all the object files in the Facebook SDK, including the FBLoginView class. If you want to know more about what the -ObjC flag does, you can check out our troubleshooting guide.
It mentioned the FBLoginView, but according to the answer to this question, it also works for FBProfilePictureView: FBProfilePictureView object wont show image
Hope this helps.

Unrecognized selector error when calling a super's initWithAttributedString: method from an NSAttributedString subclass

I'm tearing my hair out trying to do the simplest of tasks... subclassing an NSAttributedString. Trying to call the super class's initWithAttributedString: method is causing an unrecognized selector sent to instance error.
MODAttributedString.h:
#import <Foundation/Foundation.h>
#interface MODAttributedString : NSAttributedString
#property (nonatomic, retain) NSDictionary *links;
+ (MODAttributedString*) attributedStringWithFormat:(NSString*)text args:(id)argOne, ... NS_REQUIRES_NIL_TERMINATION;
+ (MODAttributedString*) attributedStringWithFormat:(NSString*)text attributes:(NSDictionary*)attributeDict;
#end
The code that is causing the crash (I'll explain the reason I split the alloc from the init in a moment):
MODAttributedString *modString = [MODAttributedString alloc];
// Pausing debugger here and typing 'po modString' causes gdb error
modString = [modString initWithAttributedString:attributedString];
My only clue is that stepping over the alloc call, when I try to po modString, I'm given this error:
"The program being debugged hit an ObjC exception while in a function called from gdb.
If you don't want exception throws to interrupt functions called by gdb
set objc-exceptions-interrupt-hand-call-fns to off.
GDB has restored the context to what it was before the call.
To change this behavior use "set unwindonsignal off"
Evaluation of the expression containing the function (_NSPrintForDebugger) will be abandoned."
If I temporarily change the super class of MODAttributedString to a UIView, the alloc does not cause the gdb error (I stop the debugger prior to the init, which would obviously not work for anything other than an attributed string). However, common classes like NSArray, NSDictionary and NSAttributedString all fail with the same error.
In the method that calls the [MODAttributedString alloc] I use NSAttributedString as its own standalone class just fine. I am sure I'm including the MODAttributedString header in this .m file as well.
I'm using Xcode 4.2 and the iPhone 5 simulator. I've cleaned the project multiple times, tried creating a new project, tried using both LLVM GCC 4.2 and Apple LLVM 3.0, restarted Xcode and restarted my machine all to no success. I searched for this particular issue heavily before posting, but I only found issues related to properties, never to a superclass's public methods.
Is this a build settings issue? A configuration error? A compiler bug? I've subclassed common Apple classes hundreds of times, and for some reason this is the first time I've ever had an issue. Has anyone else ever had a similar problem? It's probably a really simple fix, but I just can't seem to figure it out on my own.
Thanks in advance!
It isn't "the simplest of things". You can't subclass NSAttributedString - it's part of a class cluster. This means (among other things) that the class returned when you instantiate is not necessarily the class you asked for. See What exactly is a so called "Class Cluster" in Objective-C?
It is possible, with great pain and difficulty, to subclass within a class cluster, but my advice is to write a wrapper class instead; you'll be much happier. Cocoa's dynamic redirection of unhandled methods makes this very easy.