0

I have made a swift framework that also includes Objective-C Classes. In my test app (a separate project) if I set build destination to "Generic iOS Device" I can access swift classes and project compiles without issues. If I set destination to any simulator all .Swift classes in the framework are not visible nor accessible in code and I get "Use of undeclared type", while Obj-C classes are accessible.

Using XCode 7.2

Thanks in advance.

Edit: Could it be because of this STO Answer?

Community
  • 1
  • 1
JustADev
  • 258
  • 1
  • 8
  • 19
  • http://stackoverflow.com/questions/34524547/cant-access-my-swift-classes-into-objective-c-application/34525435#34525435 – RYANCOAL9999 Jan 08 '16 at 09:08
  • @RYANCOAL9999 I checked, and what you mentioned in the link is already done in my project. In my case I have swift app, that is using a custom framework which has swift and objective-c, and not trying to use Swift in Obj-C project. – JustADev Jan 08 '16 at 09:11

2 Answers2

0

I have used Alamofire(swift framework) to try your case, it is work on swift, but it is not work on objective-c. First thing, In Build Settings, Search Enable Modules, We cannot change the module use swift framework rather than Objective-c and C. On the other hands, I can not find any method can use in objective-c, such as fuction, variable. It can know the AlamofireVersionNumber and AlamofireVersionString. It may be other(swift framework) is supporting limited in objective-c(the class must be inhertence for NSObject). https://developer.apple.com/library/ios/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html#//apple_ref/doc/uid/TP40014216-CH10-ID136

This website has said that it can use Generics,Tuples,Enumerations defined in Swift without Int raw value type,Structures defined in Swift,Top-level functions defined in Swift,Global variables defined in Swift,Typealiases defined in Swift,Swift-style variadics,Nested types,Curried functions with objective-c, Although the class should inhertence for NSObject.You cannot subclass a Swift class in Objective-C.

  • I am not trying to subclass swift in objective-c, what I have is a swift framework which also contains Obj-C classes, I am trying to run sample project on a simulator and the framework is causing an issue. However, I ran the same project on a device and everything worked. So I was wondering if it has to do due to the difference between Simulator and Read Device – JustADev Jan 11 '16 at 06:15
0

Solved my problem thanks to this gist

It turned out I need to run a script after building the framework so that iPhone and iPhone Simulator files are included in the framework. However; iPhone simulator files must be removed when submitting to app store.

Script I used from link above:

#!/bin/sh

UNIVERSAL_OUTPUTFOLDER=${BUILD_DIR}/${CONFIGURATION}-universal

# make sure the output directory exists
mkdir -p "${UNIVERSAL_OUTPUTFOLDER}"

# Step 1. Build Device and Simulator versions
xcodebuild -target "${PROJECT_NAME}" ONLY_ACTIVE_ARCH=NO -configuration ${CONFIGURATION} -sdk iphoneos  BUILD_DIR="${BUILD_DIR}" BUILD_ROOT="${BUILD_ROOT}" clean build
xcodebuild -target "${PROJECT_NAME}" -configuration ${CONFIGURATION} -sdk iphonesimulator ONLY_ACTIVE_ARCH=NO BUILD_DIR="${BUILD_DIR}" BUILD_ROOT="${BUILD_ROOT}" clean build

# Step 2. Copy the framework structure (from iphoneos build) to the universal folder
cp -R "${BUILD_DIR}/${CONFIGURATION}-iphoneos/${PROJECT_NAME}.framework" "${UNIVERSAL_OUTPUTFOLDER}/"

# Step 3. Copy Swift modules from iphonesimulator build (if it exists) to the copied framework directory
SIMULATOR_SWIFT_MODULES_DIR="${BUILD_DIR}/${CONFIGURATION}-iphonesimulator/${PROJECT_NAME}.framework/Modules/${PROJECT_NAME}.swiftmodule/."
if [ -d "${SIMULATOR_SWIFT_MODULES_DIR}" ]; then
cp -R "${SIMULATOR_SWIFT_MODULES_DIR}" "${UNIVERSAL_OUTPUTFOLDER}/${PROJECT_NAME}.framework/Modules/${PROJECT_NAME}.swiftmodule"
fi

# Step 4. Create universal binary file using lipo and place the combined executable in the copied framework directory
lipo -create -output "${UNIVERSAL_OUTPUTFOLDER}/${PROJECT_NAME}.framework/${PROJECT_NAME}" "${BUILD_DIR}/${CONFIGURATION}-iphonesimulator/${PROJECT_NAME}.framework/${PROJECT_NAME}" "${BUILD_DIR}/${CONFIGURATION}-iphoneos/${PROJECT_NAME}.framework/${PROJECT_NAME}"

# Step 5. Convenience step to copy the framework to the project's directory
cp -R "${UNIVERSAL_OUTPUTFOLDER}/${PROJECT_NAME}.framework" "${PROJECT_DIR}"

# Step 6. Convenience step to open the project's directory in Finder
open "${PROJECT_DIR}"
JustADev
  • 258
  • 1
  • 8
  • 19