Importing nested struct in swift - ios

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

Related

Why primitive View in SwiftUI has no body?

The View protocol requires a body property:
public protocol View {
associatedtype Body : View
#ViewBuilder var body: Self.Body { get }
}
Why have some built-in Views in SwiftUI no body?
#frozen public struct EmptyView : View {
#inlinable public init()
public typealias Body = Never
}
#frozen public struct VStack<Content> : View where Content : View {
#inlinable public init(alignment: HorizontalAlignment = .center, spacing: CGFloat? = nil, #ViewBuilder content: () -> Content)
public typealias Body = Never
}
have no body at all..
let emptyView = EmptyView().body
// Value of type 'EmptyView' has no member 'body'
let vStackView = VStack { Text("some text")}.body
// Value of type 'VStack<Text>' has no member 'body'
How are these Views implemented?
Each primitive view does have a body, but it isn't directly accessible at compile-time. The official Swift forums have a thread about this topic. I'll reproduce my analysis here.
Let's consider Text.
A Text value has a body property, as all Views do. Text's body calls fatalError:
import SwiftUI
func body<V: View>(of view: V) -> V.Body { view.body }
body(of: Text("hello"))
// runtime output:
SwiftUI/View.swift:94: Fatal error: body() should not be called on Text.
However, if we attempt to access the body property directly, the compiler rejects the program:
import SwiftUI
Text("hello").body
Output:
The compiler rejects the program because SwiftUI's swiftinterface file doesn't declare a body property for Text.
Why doesn't Text's declaration include body? I don't know for sure because I don't have access to the SwiftUI source code, but I have discovered that we can use #_spi to omit a declaration from a .swiftinterface file.
I put the following into MyView.swift:
import SwiftUI
public struct MyView: View {
#_spi(Private)
public var body: Never { fatalError() }
}
Then I compile it as follows, based on the “Directly invoking the compiler” instructions found here:
swiftc MyView.swift -module-name MyLib -emit-module -emit-library -emit-module-interface -enable-library-evolution
The compiler writes MyLib.swiftinterface as follows, omitting the body declaration:
// swift-interface-format-version: 1.0
// swift-compiler-version: Apple Swift version 5.7 (swiftlang-5.7.0.123.7 clang-1400.0.29.50)
// swift-module-flags: -target arm64-apple-macosx12.0 -enable-objc-interop -enable-library-evolution -module-name MyLib
import Swift
import SwiftUI
import _Concurrency
import _StringProcessing
public struct MyView : SwiftUI.View {
public typealias Body = Swift.Never
}
And it writes MyLib.private.swiftinterface as follows, containing the body declaration:
// swift-interface-format-version: 1.0
// swift-compiler-version: Apple Swift version 5.7 (swiftlang-5.7.0.123.7 clang-1400.0.29.50)
// swift-module-flags: -target arm64-apple-macosx12.0 -enable-objc-interop -enable-library-evolution -module-name MyLib
import Swift
import SwiftUI
import _Concurrency
import _StringProcessing
public struct MyView : SwiftUI.View {
#_spi(Private) #_Concurrency.MainActor(unsafe) public var body: Swift.Never {
get
}
public typealias Body = Swift.Never
}
So my best guess is that SwiftUI applies the #_spi attribute to the body property of each primitive View.
I'm not an expert but this seems very logical. Imagine that no views are offered by SwiftUI and you want to create the very first view. This view has the computed property body that is expecting you to a return a type that conforms to the View protocol (i.e. should have the body property). This will go forever. Hence, there has to be a View without the body property.

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

Use of undeclared type 'Object'

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"
}
}

Resources