Unknown class in interface builder file yet class exists - ios

This question has been asked before but I can't see any cause relevant to my issue.
I've created a very simple view controller and trying to load it but getting an unknown class MapViewController in interface builder file error. Here are some relevant parts:
class MapViewController: UIViewController {
...
}
...
let storyboard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let mapViewController = storyboard.instantiateViewControllerWithIdentifier("MapViewControllerID") as MapViewController
The MapViewController.swift file is included in the Compile Sources.
The storyboard has the default name of Main.storyboard
Have exited Xcode, rebuilt, deleted the app on the device.
The problem still persists, any suggestions?
(Using Xcode beta 4)

My problem was I had accidentally selected a module under the class entry for custom class in IB. Once I deselected that everything worked.

How I solved this was to
1) Delete the derived data
2) Rename the class to MyMapViewController
3) Delete derived data again
4) Rename the class back to MapViewController

Related

UnitTest in swift | IOS Storyboard | Failed to cast viewcontroller class defined in objective c

So I am trying to write down some test cases for one of my controller class,
My viewcontroller is defined in objective c.
I am writing test cases in swift.
I have included my viewcontroller class in both my main target and my test target.
I have also added the storybaord in both test and main target.
In my test I am trying to load the view controller from story board, below is the code (this code is working fine in my normal project from swift class)
let storyboard = UIStoryboard.init(name: "Main", bundle: Bundle.init(for: self.classForCoder))
let viewController:UIViewController = storyboard.instantiateViewController(withIdentifier: "loginViewController")
print(viewController.classForCoder)
print(type(of: viewController))
let _ = viewController.view
controller = viewController as! LoginViewController
print output
LoginView Controller
__ABT_LoginViewController
Last line of my code is getting failedi.e
controller = viewController as! LoginViewController
error I am getting below
Could not cast value of type '__ABT_LoginViewController' (0x600000053b30) to 'LoginViewController' (0x11e88de30).
Need your help guys
Updated
So above issue resolved after following the answer by #dashandrest, Now I am facing new issue, I have created a TestAppdelegate file for test cases, in my TestAppDelegate class I have this statement import "App-swift.h now after compiling the whole code it is giving an error inside App-swift.h file mentioning cannot import module AppName, My main target module name is AppName whereas test target module name is AppNameTest,
#import "App-swift.h"
Now I am getting compiling error inside App-swift.h file.
#if defined(__has_feature) && __has_feature(modules)
#import ObjectiveC;
#import AppName; ///this line generate compile errror, module AppName not found
#import XCTest;
#endif
Updated
So there were multiple issues in my app which I resolved, definitely #DashAndRest input help me in identifying those, below are the things I correct in order to make things work.
Remove all code files from test target except the test cases.
Make sure that I have separate Module name for my main target and test target.
Make sure that My Pods file does have installing the libraries for my test target also.
Write test cases in Swift for the safe side so that I don't need to include Appname-swift.h in test cases.
Use #testable import AppModuleName (as suggested by #DashAndRest)
This is not required:
3 I have included my viewcontroller class in both my main target and my test target.
4 I have also added the storybaord in both test and main target.
Try unchecking Test target for both view controller and storyboard.
And then in your test file just include main target as:
#testable import Your-Main-Target-Name
Update
Still App-swift.h is confusing.
But if you want to test LoginViewController, here is the solution:
Add Bridging Header to your main AppName target and import all the objective-c classes to it, like:
#ifndef AppName_Bridging_Header_h
#define AppName_Bridging_Header_h
#import "LoginViewController.h"
#endif
After this, your LoginViewController class will be available for all other swift classes to use, including test target.
Add new file like LoginVCTests.swift in test target and add following code to it-
import XCTest
#testable import AppName
class LoginVCTests: XCTestCase {
func testSomeLogic() {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let viewController:UIViewController = storyboard.instantiateViewController(withIdentifier: "loginViewController") as! LoginViewController
print(viewController.classForCoder)
print(type(of: viewController))
let _ = viewController.view
//test on viewController
}
}
Hope this will solve your problem!

Add Playground to a Storyboard project: Could not find a storyboard ... in bundle NSBundle

I have a working iOS project with Storyboards. Then I added a playground file to it with following content:
The
import UIKit
import PlaygroundSupport
import SpriteKit
var str = "Hello, playground"
let gameViewController = GameViewController.loadFromStoryboard() // <<<<- Fails
PlaygroundPage.current.liveView = gameViewController
I am getting following error at line, where I loadFromStoryboard():
NSInvalidArgumentException', reason: 'Could not find a storyboard named 'Main' in bundle NSBundle
What should I fix?
There are tonnes of questions on stackoverflow with the above title/contents however, each has a very specific scenario. I have gone through many of them over past few days and I think that my scenario is distinct due to use of Playground hence I am posting again.
If this is in an existing project, you can instantiate the storyboard by using your full bundle identifier, as demonstrated here:
let myBundle = Bundle(identifier: "com.asad.MyProgram")
let storyboard = UIStoryboard(name: "MyStoryboard", bundle: myBundle)
Please note that this does require that the playground is in the same workspace as the project with that particular bundle identifier.

Cannot find (secondary) storyboard named Test.storyboard

I would like to load a secondary storyboard Test.storyboard (next to Main.storyboard) in my app like so:
let storyboard = UIStoryboard(name: "Test.storyboard", bundle: nil)
Test.storyboard resides inside a group Test, it is marked for localization as Base (hence its Location Relative to Group is reported as Base.lproj/Test.storyboard) and it also marked for target membership in my app target. When I run the app on the simulator, I can confirm that a file Base.lproj/Test.storyboardc exists inside the app's main bundle.
Yet the above code produces this error:
Could not find a storyboard named 'Test.storyboard' in bundle ...
What is the right way to successfully package and load a secondary storyboard like mine?
You don't need to load storyboard with .storyboard extension .use like this
let storyboard = UIStoryboard(name: "Test", bundle: nil)

Using storyboards for developing a static library in iOS

I am a new iOS developer. I have to develop a static library. I have number of viewcontrollers and the flows between them are defined using a story board. I have created a two targets from my project. One is a static library and the other is a bundle. I use my bundle to include my story board in it. I have used this code to launch my first viewcontroller from the storybaord.
-(void)showFromViewController:(UIViewController *)vc{
NSBundle * bundle = [NSBundle bundleWithURL:[[NSBundle mainBundle] URLForResource:#"storyBoards" withExtension:#"bundle"]];
UIStoryboard * libStoryBoard = [UIStoryboard storyboardWithName:#"Main1" bundle:bundle];
ViewController1 * viewController = [libStoryBoard instantiateViewControllerWithIdentifier:#"ViewController1"];
[vc presentViewController:viewController animated:YES completion:NULL];
}
vc is a ViewController that calls this function.
And yes ViewController has target membership with the static library.
However after including the '.a' and '.bundle' in another project and calling the above function it throws an error in the console Log.
Unknown class ViewController1 in Interface Builder file.
Can anyone suggest meright way to use a story board along with a static library. And if Yes then can you point out the right way to launch the first viewcontroller from the storyboard who's class is included in the static library.
simply you can navigate like this
LoginViewController *loginVC=[self.storyboard instantiateViewControllerWithIdentifier:#"loginView"];
[self presentViewController:loginVC animated:YES completion:nil];
I have a similar setup. I have my main project, and I have a ModelController sub project which has a static library and a resources bundle that are used in my main project.
I created a storyboard and a test view controller and used your method above to load.
The only things that might catch you out are in your static library > target > build phases > copy headers > ensure you add your ViewController1.h file into this list.
Other than that, everything worked fine for me.
One possible cause here is that the view controller class (implemented in the static library) is stripped out by the linked, and therefore unavailable to the storyboard loader at runtime. I ran into this problem, and it went away when I made sure that the class in question was referenced from the code that was using the storyboard.
In my case that was easy because I needed to reference the class anyway (I just hadn't written that code yet), but I verified that [ViewController class] was enough to prevent the linker from erroneously stripping the class out.

'Use of Unresolved Identifier' in Swift

So I have been making an app, and everything has been working great. But today I made a new class like usual and for some reason in this class I can't access Public/Global variable from other classes. All the other classes can, but now when ever I try to make a new class I can't. How would this be fixed?
I am using Swift and Xcode 6.
Working Class:
import UIKit
import Foundation
import Parse
import CoreData
var signedIn = true
class ViewController: UIViewController {
New Class:
import UIKit
class NewClass: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
signedIn = false
}
But on signedIn = false
I get the error:
use of unresolved identifier "signedIn"
One possible issue is that your new class has a different Target or different Targets from the other one.
For example, it might have a testing target while the other one doesn't. For this specific case, you have to include all of your classes in the testing target or none of them.
Once I had this problem after renaming a file. I renamed the file from within Xcode, but afterwards Xcode couldn't find the function in the file. Even a clean rebuild didn't fix the problem, but closing and then re-opening the project got the build to work.
'Use of Unresolved Identifier' in Swift my also happen when you forgot to import a library. For example I have the error:
In which I forgot the UIKit
import UIKit
Sometimes the compiler gets confused about the syntax in your class. This happens a lot if you paste in source from somewhere else.
Try reducing the "unresolved" source file down to the bare minimum, cleaning and building. Once it builds successfully add all the complexity back to your class.
This has made it go away for me when re-starting Xcode did not work.
Another place I've seen this error is when your project has multiple targets AND multiple bridging headers. If it's a shared class, make sure you add the resource to all bridging headers.
A good tip to is to look in the left Issue Navigator panel; the root object will show the target that is issuing the complaint.
For me this error happened because I was trying to call a nested function. Only thing I had to do to have it fixed was to bring the function out to a scope where it was visible.
In my case, I had an Object-C file which was also in the same Target Membership. I fixed by adding #import "YourObjectCFileHeader.h" inside file Bridging-Header.h
Because you haven't declared it. If you want to use a variable of another class you must use
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
var DestViewController : ViewController = segue.destinationViewController as ViewController
DestViewController.signedIn = false
}
You have to put this code at the end of the NewClass code
Your NewClass inherits from UIViewController. You declared signedIn in ViewController. If you want NewClass to be able to identify that variable it will have to be declared in a class that your NewClass inherits from.
I got this error for Mantle Framework in my Objective-Swift Project.
What i tried is ,
Check if import is there in Bridging-Header.h file
Change the Target Membership for Framework in Mantle.h file as shown in below screenshot.
Toggle between Private Membership first build the project , end up with errors.
Then build the project with Public Membership for all the frameworks appeared for Mantle.h file, you must get success.
It is just a bug of building with multiple framework in Objective-C Swift project.
If this is regarding a class you created, be sure that the class is not nested.
F.e
A.swift
class A {
class ARelated {
}
}
calling var b = ARelated() will give 'Use of unresolved identifier: ARelated'.
You can either:
1) separate the classes if wanted on the same file:
A.swift
class A {
}
class ARelated {
}
2) Maintain your same structure and use the enclosing class to get to the subclass:
var b = A.ARelated
I did a stupid mistake. I forgot to mention the class as public or open while updating code in cocoapod workspace.
Please do check whether accesor if working in separate workspace.
You forgot to declare the variable. Just put var in front of signedIn = false
My issue was calling my program with the same name as one of its cocoapods. It caused a conflict. Solution: Create a program different name.
This is not directly to your code sample, but in general about the error. I'm writing it here, because Google directs this error to this question, so it may be useful for the other devs.
Another use case when you can receive such error is when you're adding a selector to method in another class, eg:
private class MockTextFieldTarget {
private(set) var didCallDoneAction = false
#objc func doneActionHandler() {
didCallDoneAction = true
}
}
And then in another class:
final class UITextFieldTests: XCTestCase {
func testDummyCode() {
let mockTarget = MockTextFieldTarget()
UIBarButtonItem(barButtonSystemItem: .cancel, target: mockTarget, action: MockTextFieldTarget.doneActionHandler)
// ... do something ...
}
}
If in the last line you'd simply call #selector(cancelActionHandler) instead of #selector(MockTextFieldTarget.cancelActionHandler), you'd get
use of unresolved identifier
error.

Resources