Swift 框架
Swift Framework
我制作了一个 swift 框架,其中还包括 Objective-C 类。
在我的测试应用程序(一个单独的项目)中,如果我将构建目标设置为“Generic iOS Device
”,我可以访问 swift 类 并且项目编译没有问题。
如果我设置目标 to any simulator
框架中的所有 .Swift 类 在代码中不可见或不可访问,我得到“Use of undeclared type
”,而 Obj-C 类 可访问。
使用 XCode 7.2
提前致谢。
编辑:
难道是因为of this STO Answer?
我已经使用 Alamofire(swift 框架) 来尝试你的案例,它在 swift 上有效,但在 objective-c 上无效。首先,在构建设置中,搜索启用模块,我们不能更改模块使用 swift 框架而不是 Objective-c 和 C。另一方面,我找不到任何可以在 [=15 中使用的方法=],比如函数,变量。它可以知道 AlamofireVersionNumber 和 AlamofireVersionString。它可能是其他(swift 框架)支持限制在 objective-c(class 必须是 NSObject 的继承)。 https://developer.apple.com/library/ios/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html#//apple_ref/doc/uid/TP40014216-CH10-ID136
本网站表示可以使用 Swift 中定义的泛型、元组、枚举而不使用 Int 原始值类型、[=31= 中定义的结构]、Top-level 中定义的函数 Swift、Swift 中定义的全局变量、Swift 中定义的类型别名、Swift-style 变量、嵌套类型、带有 objective-c 的柯里化函数,尽管 class 应该NSObject.You 的继承不能在 Objective-C.
中子 class 一个 Swift class
解决了我的问题谢谢to this gist
原来我需要在构建框架后 运行 脚本,以便 iPhone 和 iPhone 模拟器文件包含在框架中。然而; iPhone 提交到应用商店时必须删除模拟器文件。
我在上面 link 中使用的脚本:
#!/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}"
我制作了一个 swift 框架,其中还包括 Objective-C 类。
在我的测试应用程序(一个单独的项目)中,如果我将构建目标设置为“Generic iOS Device
”,我可以访问 swift 类 并且项目编译没有问题。
如果我设置目标 to any simulator
框架中的所有 .Swift 类 在代码中不可见或不可访问,我得到“Use of undeclared type
”,而 Obj-C 类 可访问。
使用 XCode 7.2
提前致谢。
编辑: 难道是因为of this STO Answer?
我已经使用 Alamofire(swift 框架) 来尝试你的案例,它在 swift 上有效,但在 objective-c 上无效。首先,在构建设置中,搜索启用模块,我们不能更改模块使用 swift 框架而不是 Objective-c 和 C。另一方面,我找不到任何可以在 [=15 中使用的方法=],比如函数,变量。它可以知道 AlamofireVersionNumber 和 AlamofireVersionString。它可能是其他(swift 框架)支持限制在 objective-c(class 必须是 NSObject 的继承)。 https://developer.apple.com/library/ios/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html#//apple_ref/doc/uid/TP40014216-CH10-ID136
本网站表示可以使用 Swift 中定义的泛型、元组、枚举而不使用 Int 原始值类型、[=31= 中定义的结构]、Top-level 中定义的函数 Swift、Swift 中定义的全局变量、Swift 中定义的类型别名、Swift-style 变量、嵌套类型、带有 objective-c 的柯里化函数,尽管 class 应该NSObject.You 的继承不能在 Objective-C.
中子 class 一个 Swift class解决了我的问题谢谢to this gist
原来我需要在构建框架后 运行 脚本,以便 iPhone 和 iPhone 模拟器文件包含在框架中。然而; iPhone 提交到应用商店时必须删除模拟器文件。
我在上面 link 中使用的脚本:
#!/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}"