Xcode 7.3 Syntax Highlighting and Code Completion issues with Swift - ios

I am having an extremely frustrating issue with XCode 7.3 (however, this issue has persisted since I installed XCode 7.2) and Swift code, and I am hoping others have had this issue and know how to resolve it. Syntax highlighting and code completion work perfectly fine in Objective-C files, and also works fine when calling other Swift objects within Swift code. However, any Objective-C objects or methods mentioned in Swift code get no syntax highlighting, and XCode will not complete ANY Objective-C declared methods or properties. Everything compiles and runs just fine.
I should also add that I have also tried doing a completely clean install of XCode. I deleted all my derived data, deleted all XCode caches, and deleted my XCode preferences files (in addition to obviously deleting the XCode.app archive before re-installing).
It is making it extremely difficult to develop in Swift. I don't want to do this, but if I can't find a way to resolve this I'll be forced to go back to using Objective-C.

I have the same problem. But finally solved it.
I make two change, not sure which is the key point but you can try them all.
delete the module cache
Within the same folder as your project's Derived Data is a Module
Cache. When Code Completion stopped working, deleting this fixed it.
Close Xcode and delete the
~/Library/Developer/Xcode/DerivedData/ModuleCache directory.
change the Enable Modules value
Go to the Build Settings of your target, then search Enable
Modules
If it's Yes, change it to No, and you may get some build
error, just change it back to Yes.
After two steps above you should Clean(Shift+Command+K) your project.
For now you may fixed the problem.

So it seems the issue was with CocoaPods. I was using Cocoapods as a static library instead of as frameworks. Switching to frameworks (using use_frameworks! in my Podfile) and importing the libraries into Swift has resolved all my issues. I'm guessing all those third party library headers were just too much for XCode to process. Either way, the issue is now resolved. I hope this helps someone in the future.

This might not be necessary anymore but i still want to post this:
At the time of this post, the most recent version of cocoapods (1.0.0.beta.8) requires you to define pods for each Xcode target.
In my case I had a class compile for the project target and for a testing target. I added a pod only to the main target, because of laziness.
Now working in the code of class A I added the pod framework using import NAME and tried to use the classes of the framework. Xcode wouldn't highlight the particular code where I use the new classes, but compiling and running works fine. In the completion dialog the type of the variable was <<error type>>
The way to resolve this issue: in the Podfile add the newly added pod to all targets, the class A is member of.
Now Xcode finds the necessary frameworks for all targets and code highlighting works again.
EDIT 1:
A possible solution is defining a list of shared pods, like in my example:
platform :ios, '8.4'
use_frameworks!
inhibit_all_warnings!
def all_pods
pod 'MPMessagePack'
pod 'SwiftyDispatch'
pod 'BFKit'
pod 'Timepiece'
pod 'Alamofire'
pod 'AlamofireSwiftyJSON'
end
def testing_pods
pod 'Quick'
pod 'Nimble'
end
target 'App' do
all_pods
end
target 'AppLogicTests' do
all_pods
testing_pods
end
target 'AppUITests' do
pod 'RxTest'
all_pods
testing_pods
end
post_install do |installer|
installer.pods_project.targets.each do |target|
puts target.name
end
end
This will add all pods to all targets and adding all testing pods to the targets. Next to these I added 'RxTest' to the AppUITests.
(Chosen pods are examples of my projects, no advertising intended :-) )

We had the same issue in a mixed ObjC/Swift project. Tried all the suggestions about deleting derived data etc, to no avail. Sometimes it helped, but not in a reproducible way and after some time it stopped working.
The post of Galvin in this post put me on the track of the Module related build settings. However it was another setting that solved the code completion/coloring in a reproducible way: setting DEFINES_MODULE (under Packaging) from YES to NO for our main project was the solution.
Notes:
I expected this to break the ObjC/Swift interoperability in our project, but that still works. It seems that setting is only to be used for framework targets. (https://developer.apple.com/library/ios/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html)
This project setting has not been changed for months, but the code completion issues came up only recently, both for my colleague and me.

If none of the above worked for you and you're using Cocoapods, you can try switching to Carthage.
I tried every suggestion I could find on Google to no avail. But consistently Cocoapods seemed to be coming up as a reason with many hacks in attempt to fix it. I have been reading up on Carthage, and how it doesn't modify your project, force you to use a workspace, or potentially fill your build folder with header files (which confuses Xcode and can cause the syntax highlighting and autocomplete to break).
After making the switch I haven't run into any issues yet, and to be honest I prefer the teensy bit of overhead to have a clean solution. This post really drove it home for me.

Related

Class is implemented in both when running IOS simulator

I'm using cocoapods version 1.9.1 and Xcode 11.3.1
I'm following this tutorial https://docs.snapchat.com/docs/tutorials/login-kit/ios/
I keep getting this error that says class is implemented in both when running my app on IOS simulator, the problem lies in CoreSimulator/Devices
Class SCSDKCertificateTrust is implemented in both
/Users/Developer/CoreSimulator/Devices/EB370499-8654-42DD-9FC0-
F7ACB17C0532/data/Containers/Bundle/Application/695ED9C1-3329-457C-BCED-
3670C52E4E19/Test.app/Frameworks/SCSDKLoginKit.framework/SCSDKLoginKit (0x1033dba50) and
/Users/Developer/CoreSimulator/Devices/EB370499-8654-42DD-9FC0-
F7ACB17C0532/data/Containers/Bundle/Application/695ED9C1-3329-457C-BCED-
3670C52E4E19/Test.app/Frameworks/SCSDKCoreKit.framework/SCSDKCoreKit (0x10339b098). One of
the two will be used. Which one is undefined.
My Podfile
# Uncomment the next line to define a global platform for your project
# platform :ios, '9.0'
target 'Test' do
# Comment the next line if you don't want to use dynamic frameworks
use_frameworks!
# Pods for Test
pod 'SnapSDK'
end
I'm not sure how to fix this problem and I have searched the net and found multiple solutions especially on SO
Class is implemented in both. One of the two will be used
Class is implemented in both, One of the two will be used. Which one is undefined
Unfortunately most of the solutions are about fixing it on .xcconfig file and remove in OTHER_LDFLAGS.
I tried those two solutions and it didn't work, could it be because of the cocoapods version?
because I tried this on my Xcode 10 version and still produced the same problem
For me:
Go to your Project in the top of the file navigator in Xcode and click Package Dependencies. Remove any dependencies that you may have installed using 'Add Package' instead of with cocoapods, or that may be causing the error.

Objective-C class can't import swift pod defined at top level

Here's my Podfile:
platform :ios, '9.0'
use_frameworks!
target :Test_Swift_Import do
pod 'BFKit-Swift', '1.5.1' //Just a swift pod to provide some methods to test
pod 'SwiftySwift', '1.0.1' //and another
end
In my AppDelegate class I try to use a simple method from BFKit
BOOL isEmail = [#"not an email" isEmail];
My AppDelegate is an objective-c class, not Swift
In order to make this work I try to import the BFKit Swift module
First I try the well-known way:
//Basic <Product_Name-Swift.h>
#import <Test_Swift_Import-Swift.h>
But this SO post and this one too both say to use <ProjectName/ModuleName-Swift.h>. Because the code is in a module, this makes sense to me.
So I try #import <Test_Swift_Import/BFKit-Swift.h>
No deal...
I rename my project to TestSwiftImport in case underscores are a problem
No deal...
I try importing the module directly
<BFKit/BFKit.h>
Alas...
Relevant Xcode Build Settings
defines modules : Yes
Product Module name: TestSwiftImport
Enable Modules: Yes
Link Frameworks Automatically: Yes
Objective-C Generated Interface Header Name: TestSwiftImport-Swift.h
Objective-C Bridging Header: TestSwiftImport/TestSwiftImport-Bridging-Header.h
Drastic Things I've Tried
I've done all manner of cleaning project and build folder, wiping derived data, resetting simulator, quitting simulator, quitting Xcode and restarting machine, all to no avail. It's sad that these are even things I think of doing and that they sometimes work... But not in this case.
Github Demo Project Displaying Problem
...is here
Any ideas? There are even more failed attempts that you can see if you open the project. This can't be this hard...
I see that is problem of CocoaPods.
Now If I try drag file straight to xcode it will work normal. If you really need it. You can drag all file to project. Not use cocoa-pods now.
You can reference to this question: Question
Hope this help!

Swift import module

I am new to developing iOS apps, so I'm still on the learning curve and I am experiencing quite a strange problem.
I have a project in which I am using the "Alamofire" module with CocoaPods. Everything is fine and working so far. Then I created another controller in which I want to import the same module, but the resulting message is always "No such module..."
How and why is it working in one file, but does not work in the other? Both files are in the same directory.
P.S. I've tried cleaning the project and also deleting the "DerivedData" folder of Xcode.
Check your both files Target Membership. It needs to be set to the target you are using:
Likely, it seems you dragged file into your project and did not set target properly. Check it twice, because there is no other reason for that. Unless your second file is in Test target. In that case add to your pod file:
target 'YourTestTarget' do
pod 'Alamofire'
end
and run pod install

Xcode 7 can't find header files from framework

I'm trying to add PassSlot into my project, but it says it can't find the .h file. I'm following everything correctly from here: https://github.com/passslot/passslot-ios-sdk
Is this an Xcode 7 problem? It was working fine and now that I opened the project in Xcode 7, it is giving me this problem. I reopened it on Xcode 6 and it starts showing the problem as well.
I think Dinesy is right. This solves the problem for me.
I've noticed that Xcode7 doesn't automatically fill in the required
Framework search paths when you import a 3rd party one (I believe
Xcode6 did do this). Check if yours are empty by going to Project ->
Build Settings -> Search Paths -> Framework Search Paths. Fill it in
with wherever your Frameworks live. If it's under your project you can
use $(PROJECT_DIR)
Replacing #import "Headerfile.h" with #import <Framework/Headerfile.h> worked for me.
When you Drag & Drop the required framework to your Frameworks folder, Tick on "Destination: Copy items if needed"
Then you would be able to reference it properly now.
Screenshot
Just ran into this with Xcode 7 and I ended up having to copy the 3rd party Framework (and bundle) file into my project's main directory before dragging it into the Xcode project. This allowed it to add the correct Framework search path and no longer gave me any problems.
Just hit this problem myself after making a new test target in Objective-C.
One thing to remember is that under some circumstances, each test target must be listed in the Podfile with pod dependencies. If the Podfile only associates the project with the pods, it may not find the pod header files.
Here's an example of a more complex Podfile from the cocoapods docs.
target 'MyApp' do
pod 'ObjectiveSugar', '~> 0.5'
target "MyAppTests" do
inherit! :search_paths
pod 'OCMock', '~> 2.0.1'
end
end
post_install do |installer|
installer.pods_project.targets.each do |target|
puts "#{target.name}"
end
end
I ran into this error when trying to link to my own custom framework. The problem was that I hadn't set up my framework to export the needed headers. To do so:
Select each header file you want to make available, go to the File Inspector > Target Membership, and make sure the target is checked and set to "Public".
In the MyFrameworkName.h top-level file, import each header file you want to make available: e.g. #import <MyFrameworkName/MyPublicHeader.h>
In my case, using CocoaPods, Xcode was building fine, but a command line build couldn't locate the framework headers.
The solution was to build the workspace, not the project!
Cleaning the project and restarting XCode sometimes helps

XCode keeps forgetting imported Frameworks

I have Xcode 6.3, using Swift, importing a Parse 1.7.1 Framework as usual (dragging, copying) and I set it up in a group: Frameworks.
I compile and everything works fine for a while with it, until the compiler does not recognize this sentence anymore:
import Parse
It gives me the error:
No such module 'Parse'
A workaround is to delete the Framework and copy it again, but after a while it starts getting annoying, and I would really like to know the cause.
I only code and build in the meantime (and occasionally creating new swift files), so I can't explain why this happens.
If you're targeting iOS 8 and above, you can tell Cocoapods to use frameworks, by putting
use_frameworks!
in your Podfile, like this example:
use_frameworks!
platform :ios, '8.0'
# Parse
pod 'Parse', '~> 1.7'
I could fix the same problem by doing so.
I just fixed this same issue today with my project. I imported my obj-c framework in a swift project and it worked for a while, then xcode seemed to forget it causing the same error you have.
apple docs
I fixed it by referencing the bridging header in Build Settings.
Under Build Settings, make sure the Objective-C Bridging Header build
setting under Swift Compiler - Code Generation has a path to the
header. The path should be relative to your project, similar to the
way your Info.plist path is specified in Build Settings. In most
cases, you should not need to modify this setting.
I just typed in the name of the bridging header folderName/xxxx-BridgingHeader.h in the field that states bridging header and all was well again.

Resources