Why is type undeclared? - swift

I have a very simple example that I can't get to work. Both classes are in the same iOS project. I get this error when I build:
Use of undeclared type 'classB'
It happens in classA on the static var line.
import UIKit
class classB: NSObject {
var temp:Int?
}
import UIKit
class classA: NSObject {
static var classBList:[classB]?
}
Any idea what I'm doing wrong?

The reason why you might get this error is if the ClassB file is not included in the project. If classA and classB are in separate files then verify that classB is included in the project. The reason why you are getting this error is because the compiler is not finding the class definition of classB. This can happen if the file is not included in the target.

Related

Cannot find protocol declaration for protocol in Swift-Headers.h defined in Package

I got a compilation error.
XCode did not see the protocol that had been defined in a Package.
It was used as a requirement for a class.
class TestClass: NSViewController, ProtocolFromPackage {
}
In Swift-Headers.h I got an error:
< path to compilation directory >/DerivedSources/Swift-Headers.h:5911:118: Cannot find protocol declaration for 'ProtocolFromPackage'; did you mean 'SomeOtherProtocol'?
Just add a random dummy protocol with a property with the type of protocol that causes the error. Dont forget to use #objc with the dummy protocol.
#objc protocol PleaseJustFixCompilation: AnyObject {
/*
It's needed just to let Swift-header know that ProtocolFromPackage from Package exists.
*/
var dummyProperty: ProtocolFromPackage? { get }
}
It might also require adding into some header:
#protocol ObservableBooleanListener;

Why can "-Wobjc-literal-conversion" be ignored by the preprocessor?

I had a small bug in my project probably related to -Wobjc-literal-conversion.
There's a Swift class which is later called in ObjcC code:
#objc(SomeClass)
public final class SomeClass: NSManagedObject {
}
extension SomeClass {
#NSManaged public var someProperty: Bool
}
And then somewhere in the Objc code there was a bug like this:
someClassObj.someProperty = #NO;
Here, #NO is a literal of String type. And it seems a literal is converted to true. So we had a bug.
So I've tried to treat -Wobjc-literal-conversion as error. But it doesn't work neither in the project settings:
nor with #pragma clang diagnostic error "-Wobjc-literal-conversion" at the top of the Objc file.
I didn't try to create a toy project, because this bug can be related to CoreData, so it's time-consuming.
Do you have any ideas/suggestions how to check why this warning doesn't work? I've tried to create other errors in this file intentionally - the preprocessor works in such case.
The relevant warning is -Wint-conversion.
To create a MCVE, you do not need some complicated CoreData project. In your blank project, define a Swift class:
#objc class Foo: NSManagedObject {
#NSManaged var bar: Bool
}
And then write code to update that from Objective-C:
// Baz.m
#import "Baz.h"
#import "MyApp-Swift.h"
#implementation Baz
- (void)qux:(Foo *)foo {
foo.bar = #NO;
}
#end
When you compile that, you'll get the the warning you expected.
If you go to the “Report navigator”, click on the build, and then expand the warning, you will see the compiler’s warning code:
/.../MyApp/Baz.m:14:13: warning: incompatible pointer to integer conversion assigning to 'BOOL' (aka 'signed char') from 'NSNumber *' [-Wint-conversion]
foo.bar = #NO;
^ ~~~
1 warning generated.
Once you know it is the “integer to pointer conversion” error, it is easy to find it in the build settings.

NSClassFromString #interface for 'GIDSignIn' declares the selector 'fetchCloudValues' compiler error

I'have a project where i have multiple targets, and in one of those targets i have added a custom class to download some Values from Firebase Remote Config.
Custom class is declared as follows:
#objc (Test)
final class CheckUpdate: NSObject {
#objc static let sharedInstance = CheckUpdate()
#objc func fetchCloudValues() {
// Grab remote config values and do things
}
}
I've added this class as target membership only on one target and in the code I've add
Class asdasd = NSClassFromString(#"Test");
if (asdasd) {
[[asdasd sharedInstance] fetchCloudValues];
}
My issue is that, at line where I call fetchCloudValues I get a compiling error saying that:
no visible #interface GIDSignIn declares the selector fetchCloudValues
but it's obviously an issue because asdasd should not be of type GIDSignIn

Issue with swift framework import

I have created a swift framework.
In this framework i have some swift file and one category in objective-C (so there is the .h and .m files).
I successfully build my framework, but when i import it into another project, only the method from my category (written in objective-C) are visible. If i try to use any swift class i got an error "Use of unresolved identifier".
I have checked the following points in my framework project :
all my swift class are public public class Toto
i have set the build settings - packaging - defines module param to yes
In my project where the framework is imported, i have the following code :
import UIKit
import myframework
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
toto()
}
func toto() {
let data = NSData()
data.isGzippedData()
var client = Client ()
NSLog("ok");
}
}
I have an error "Use of undeclared identifier" for the line var toto = Client (), but in my swift framework the class Client is public and its default constructor is public.
However, if i comment this line, the code works fine, even if the method isGzippedData is declared and implement in my framework (but in objective-C).
How can i use the swift class from my framework into my project ?

'unresolved identifier' for return value of Type Method in Swift

I'm trying to access the return value from a Type Method in one file from another file. To wit:
file_1:
class LetterView: UIView {
class func testFunction() -> CGSize {
return CGSizeMake(100,200)
}
}
file_2:
class AnotherClass {
func callTestFunction() {
var result = LetterView.testFunction()
print("- breakpoint here - ")
}
}
I get an Unresolved Identifier error on var result if I put a breakpoint in the debugger and do a po result. However if I change the return type of testFunction() to be an Int (say 2) and return that instead, then the function call works as expected. Color me confused.
Is the second file importing UIKit as well? Also, you should update your example from function to func. This all works in the playground which leads to UIKit missing.
There could be a few possible issues.
One of the classes has a Testing target and other one doesn't. You have to even include all of your classes in the testing target or none of them.
If it's Objective C class, check that the class is in ObjectiveC bridging header file.
If it's NSManagedObject subclass. Add #objc(className) before the class declaration.
If it's part of a different framework, make sure that the class or function is public
This is the original answer link : Swift Compiler Error: Use of unresolved identifier 'name'