Unable to access Swift 4 class from Objective-C: Property not found on object of type
The rules for exposing Swift code to Objective-C have changed in Swift 4. Try this instead:
@objc var foobar = true
As an optimization, @objc
inference have been reduced in Swift 4. For instance, a property within an NSObject
-derived class, such as your TestViewController
, will no longer infer @objc
by default (as it did in Swift 3).
Alternatively, you could also expose all members to Objective-C at once using @objcMembers
:
@objcMembers class TestViewController: UIViewController {
...
}
This new design is fully detailed in the corresponding Swift Evolution proposal.
Cannot access property on Swift type from Objective-C
Optional values of non-Objective-C types aren't bridged into Objective-C. That is, the first three properties of TestClass
below would be available in Objective-C, but the fourth wouldn't:
class TestClass: NSObject {
var nsNumberVar: NSNumber = 0 // obj-c type, ok
var nsNumberOpt: NSNumber? // optional obj-c type, ok
var doubleVar: Double = 0 // bridged Swift-native type, ok
var doubleOpt: Double? // not bridged, inaccessible
}
In your Objective-C code, you'd access those first three properties like this:
TestClass *optTest = [[TestClass alloc] init];
optTest.nsNumberOpt = @1.0;
optTest.nsNumberVar = @2.0;
optTest.doubleVar = 3.0;
In your case, you can either convert lat
and long
to be non-Optional or switch them to be instances of NSNumber
.
Note that you need to be careful about your Objective-C code if you take the second approach (switching lat
and lon
to non-optional properties of type NSNumber
) -- while the Swift compiler will prevent you from assigning nil
to non-optional properties, the Objective-C compiler has no qualms about allowing it, letting nil
values sneak into your Swift code with no chance of catching them at runtime. Consider this method on TestClass
:
extension TestClass {
func badIdea() {
// print the string value if it exists, or 'nil' otherwise
println(nsNumberOpt?.stringValue ?? "nil")
// non-optional: must have a value, right?
println(nsNumberVar.stringValue)
}
}
This works fine if invoked with values in both of the properties, but if nsNumberVar
is set to nil
from the Objective-C code, this will crash at runtime. Note that there is no way to check whether or not nsNumberVar
is nil
before using it!
TestClass *optTest = [[TestClass alloc] init];
optTest.nsNumberOpt = @1.0;
optTest.nsNumberVar = @2.0;
[optTest badIdea];
// prints 1, 2
optTest.nsNumberOpt = nil;
optTest.nsNumberVar = nil;
[optTest badIdea];
// prints nil, then crashes with an EXC_BAD_ACCESS exception
Property not found on object of type' when creating Objective C to Swift Bridge
At first I thought your code was just some sort of misprint, but over the course of discussion in the comments I have come to realize that it is real and that you are attempting to put Swift code into an Objective-C file (namely AppDelegate.m). You cannot do that! An Objective-C file must be written entirely in Objective-C!
I would question whether you really even need a hybrid app (an app written in two languages). Your life will be much simpler if you pick just one language and write the whole app in that language. But if you are going to have a hybrid app, you need to learn Objective-C and write the Objective-C in Objective-C.
Property 'CGImage' not found on object of type 'MyClass *'
The code is (most likely) a category (in Swift nomenclature 'extension' ) on UIImage
.
Technically it would work on anything that responded to the selector CGImage
.
The full declaration should be something like:
UIImage + ColorHelp.h
@interface UIImage (ColorHelp)
- (UIColor *)averageColor;
@end
UIImage + ColorHelp.m
@implementation UIImage (ColorHelp)
- (UIColor *)averageColor {
//stuff
}
@end
Create a new "Objective-C File" in Xcode to create a template category on UIImage
.
Fromm there all instances of UIImage
will respond to average color. e.g
UIImage *myImage = <something>;
UIColor *averageColor = [myImage averageColor];
Property 'sharedInstance' not found on object of type ClassA
Finally i was able to fix this with a minor change !! :)
Swift framework code
@objc class SingletonTest: NSObject {
// swiftSharedInstance is not accessible from ObjC
class var swiftSharedInstance: SingletonTest {
struct Singleton {
static let instance = SingletonTest()
}
return Singleton.instance
}
// the sharedInstance class method can be reached from ObjC
class func sharedInstance() -> SingletonTest {
return SingletonTest.swiftSharedInstance
}
// Some testing
func testTheSingleton() -> String {
return "Hello World"
}}
Objective C parent project code
SingletonTest *aTest = [SingletonTest sharedInstance];
NSLog(@"Singleton says: %@", [aTest testTheSingleton]);
Objective-C: Property not found on object of type
container.containerName
is called dot syntax. It is the equivalent of [container containerName]
. But you don't have a property or method on your class named containerName
so the compiler is complaining.
Now, that is likely entirely baffling in the context of the book you are using. And that is because the book you are using is way way way out of date.
If it were up to date, your Item class would look more like:
@interface Item:NSObject
@property(copy) NSString *containerName;
@property(copy) NSDate *containerDateCreated;
@property(assign) NSInteger containerValueInDollars;
... etc ...
@end
Get yourself an up to date set of tutorials and/or books before learning anything else. It'll save a lot of frustration.
Objective C - IOS - Property not found on object of type - when accessing object variables
You did not inherit shopObjects
in your viewController. It is a separate object.You need to create a variable of type shopObjects
, set its properties and then call that. e.g.
shopVcSelectionViewController.h
@property (strong, nonatomic) shopObjects *shopObject;
shopVcSelectionViewController.m
...
self.shopItemTitle.text = self.shopObject.shopTitle
...
Segue from Obj-c to swift can't find property
If you are using Swift file in objc classes Bridging header won't help here.
Xcode will create Header file which you need to import
First goto build settings and search for Defines Modules and set it to true.
Now in .m
file add following header file
#import "YourTargetName-Swift.h"
and build the project
and also add @objc
before your property
Hope it is helpful
Related Topics
Swift:Missing Argument Label 'Xxx' in Call
How to Define the Size of a Collectionview on Rotate
Swiftui: Unwanted Split View on iPad
Right Aligned Uitextfield Spacebar Does Not Advance Cursor in iOS 7
iOS Provisioning and Certifcates - Will Revoke/Renew Effect App Store Apps
Swift - Coredata Migration - Set New Attribute Value According to Old Attribute Value
How to Detect Call Incoming Programmatically
Uiview Animation Jumps at Beginning
Uitextview Starts at Bottom or Middle of the Text
How to Subclass Uitableviewcontroller in Swift
Ios: Determine If Device Language Is Right to Left (Rtl)
Change Uidatepicker Font Color
Get Advertisement Data for Ble in iOS