Use of undeclared type 'Object' - ios

This is so weird. Normally I could understand a class not being declared, but this is claiming the Object class itself is not declared. NSObject works, but the way my project is setup I need it to be a pure Swift Object. My class header looks like this:
import UIKit
import Foundation
class Person: Object {
I know foundation isn't really necessary, I just added it because I wasn't sure if that was causing me issues. The issue is occurring in both actual projects and playgrounds as well as in Xcode 6 and the latest Xcode 7 beta with Swift 2.0

if you are using Realm then import
import RealmSwift
Complete syntax of class.
import UIKit
import RealmSwift
class User: Object {
}
If you face any difficulty to implement this code. Watch below video
Link: https://youtu.be/5Z8tBKwk6-c

Just remove : Object. Pure Swift classes don't need to inherit from a superclass

Pure Swift object doesn't inherit from a superclass:
class Person {}
You can read Classes and Structures

For RealmSwift
import Foundation
import RealmSwift
class Note: Object{
#objc dynamic var id = 0
#objc dynamic var title = ""
#objc dynamic var created = Date()
#objc dynamic var text = ""
override static func primaryKey() -> `enter code here`String? {
return "id"
}
}

Related

Error: Use of undeclared type BindableObject

I’m following this tutorial for SwiftUI amplify app where I came across this error when creating a final class which conforms to Bindable object.
Error:Use of undeclared type 'BindableObject'
import Combine
import SwiftUI
import AWSAppSync
final class TalkStore: BindableObject {
/*
Required by SwiftUI
*/
let didChange = PassthroughSubject<TalkStore, Never>()
var listTalks: [ListTodosQuery.Data.ListTodo.Item] {
didSet {
didChange.send(self)
}
}
//We will be using this later.
private let appSyncClient: AWSAppSyncClient!
/*
Init if running app is using SwiftUI Content View
*/
init(talks: [ListTodosQuery.Data.ListTodo.Item]) {
self.appSyncClient = nil
self.listTalks = talks
}
}
Is it possible that Apple has changed the class name?
How do I find that out?
BindableObject has been renamed ObservableObject
BindableObject is replaced by the ObservableObject protocol from the Combine framework. (50800624)
Source: https://developer.apple.com/documentation/ios_ipados_release_notes/ios_13_release_notes

Is it possible to call objective C application delegate method from Swift class of the same project?

I'm merging my Swift project with already existing Objective-C code. I need to call some important methods of Swift class from objective C app delegate. I tried all methods given in net, but it was no use. Can any one help me out?
Yes, it's possible but with some limitations.
You can use only classes which inherited from NSObject, with public attribute and marked with #objc. At Objective-C code you should import "ProductModuleName-Swift.h" file which generated by compiler.
Here is an example of Swift class:
import Foundation
#objc public class ExampleClass: NSObject {
#objc public var someInstanceProperty = "Property"
#objc public func someFunction() {
print("Some function")
}
}
Notice that this class inherited from NSObject and have #objc and public attributes. After command+B you can take a look at generated bridge header through Assistance editor:
Then you should import the bridge header at your Objective-C class.
#import "ProductModuleName-Swift.h"
And then you can use your Swift class at Objective-C code like any other Objective-C class:
__auto_type const someClass = [ExampleClass new];
[someClass someFunction];
NSLog(#"%#", someClass.someInstanceProperty);
Here is an additional information from Apple:
Importing Swift into Objective-C

Type conversion SIGABRT

I'm trying to load classes from a framework dynamically but the application crashes with conversion sending SIGABRT signal:
let newClassType = NSClassFromString("MyFramework.CustomClass") as! BaseClass.Type
With classes declared in MyApp it works fine as well as with framework's classes but without conversion.
BaseClass.swift (MyApp.xcodeproj and copied to MyFramework):
public class BaseClass{
...
}
CustomClass.swift(MyFramework.xcodeproj):
public class CustomClass: BaseClass{
...
}
What might be the problem?
From The question and the comments I suggest to use typealias
import MyFramework
typealias FrameworkBaseClass = MyFramework.BaseClass
typealias FrameworkCustomClass = MyFramework.CustomClass
Then if you want to use FrameworkBaseClass, FrameworkCustomClass in other .swift files, you do not have to import MyFramework

Importing nested struct in swift

I have a framework and a nested struct which I wish to import in my app.
open class SomeClass {
public struct SomeNestedStruct {
public let someProperty: Int
}
}
The goal is to import only SomeNestedStruct. What I have tried so far is
import struct MyFramework.SomeClass.SomeNestedStruct
The only way around it is to import the whole class.
This is impossible.
You can import a top level object (struct, enum, class, constant etc) from a module or a submodule. However, you cannot import an inner object.
According to https://docs.swift.org/swift-book/ReferenceManual/Declarations.html#grammar_import-declaration you have to
import struct MyFramework.SomeClass.SomeNestedStruct

Cannot find protocol declaration for 'ABPeoplePickerNavigationControllerDelegate'

In a Project having already ObjC, I'm adding a Swift Class
import AddressBookUI
class MyVC : UITableViewController, ABPeoplePickerNavigationControllerDelegate {
}
MyApp-Swift.h:289:42: Cannot find protocol declaration for 'ABPeoplePickerNavigationControllerDelegate'; did you mean 'UINavigationControllerDelegate'?
No, Swift, I really mean ABPeoplePickerNavigationControllerDelegate. Really wondering what I am doing wrong here...
FWIW, the reason this works with ABRecord in pure Swift, but not in the Objective-C compatibility header is that there is a typealias which the latter apparently doesn't translate back correctly:
typealias ABRecordRef = ABRecord
see https://developer.apple.com/library/prerelease/ios/documentation/AddressBook/Reference/ABRecordRef_iPhoneOS/index.html#//apple_ref/c/tdef/ABRecordRef
Might be worth filing a Radar 📡
I need to add
#import <AddressBookUI/AddressBookUI.h>
in my Briding-Header.h. One could think the import in my Swift file was enough. It was not.
That said, now I have a new problem when implementing
func peoplePickerNavigationController(peoplePicker: ABPeoplePickerNavigationController!, didSelectPerson person: ABRecord!, property: ABPropertyID, identifier: ABMultiValueIdentifier) {
}
Here comes the next error:
-Swift.h:297:110: Expected a type
It has a problem with the ABRecord type in
didSelectPerson:(ABRecord)
Doesn't help if I also import AddressBook in Briding Header or Swift File.
You can check the code I've used here
Pure Swift project, no Objective-C involved
For me, this is compiling fine without using any Bridging-Header
import UIKit
import AddressBook
import AddressBookUI
class ViewController: UITableViewController, ABPeoplePickerNavigationControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
func peoplePickerNavigationController(peoplePicker: ABPeoplePickerNavigationController!, didSelectPerson person: ABRecord!, property: ABPropertyID, identifier: ABMultiValueIdentifier) {
}
}
I'm adding relevant frameworks (AddressBook, AddressBookUI) to the link binary with libraries phase of my target
Objective-C Project, with Bridging Header
My Bridging-Header.h:
#import <AddressBook/AddressBook.h>
#import <AddressBookUI/AddressBookUI.h>
make sure your Bridging-Header is properly referenced in your target
swift VC code is the same as above
Fixed my second Problem thanks to #neonacho on Twitter. Instead of ABRecord I had to use ABRecordRef to compile. Not sure why Diego's code works and not mine. So it became
func peoplePickerNavigationController(peoplePicker: ABPeoplePickerNavigationController!,
didSelectPerson person: ABRecordRef!, property: ABPropertyID, identifier: ABMultiValueIdentifier) {
}
and it works.

Resources