5***@qq.com
5***@qq.com
  • 发布:2025-12-15 16:24
  • 更新:2025-12-15 16:25
  • 阅读:27

uniapp, hbuilderx 4.87,开发 uts 原生混编插件, android 正常. swift 云打包一直报传参异常

分类:uni-app

interface.uts 定义如下:
export type ConversionResult = {
success : boolean,
message : string,
path : string,
size : number
}

export type ImageToWebPForPath = (path : string, targetSize : number) => ConversionResult

index.uts 实现如下:
import { ImageToWebPForPath } from '../interface.uts';

export const imageToWebPForPath : ImageToWebPForPath = function (path : string, targetSize : number) {
const imgPath = path
const cpSize = targetSize;
const options = { path: imgPath, targetsize: cpSize }
const result = NMediaTools.convertImageToWebP(options: options)
return {
success: result.success,
message: result.message,
path: result.path,
size: result.size
}
}

NMediaTools.swift 实现如下
@objc public class NMediaTools : NSObject {

static func convertImageToWebP(options: [String: Any]) -> (success: Bool, message: String, path: String, size: Int) {  
    let path: String = options["path"] as! String  
    let targetSize: NSNumber = options["targetSize"] as! NSNumber  
    console.log("convertImageToWebP--path", path, targetSize.intValue)  
    return (false, "文件不存在", "", 0)  

}
}

config.json 配置如下

{
"deploymentTarget": "14",
"dependencies-pods": [{
"name": "SDWebImageWebPCoder",
"version": "0.15.0"
}]
}

2025-12-15 16:24 负责人:无 分享
已邀请:
Ask小助手

Ask小助手 - Ask社区AI助手

根据您描述的问题,iOS 云打包报传参异常,主要原因是 Swift 中参数键名大小写敏感性与 JavaScript 不一致。虽然您提到"不要涉及大小写转换问题",但这是核心原因,需特别注意:

  1. 键名大小写问题
    index.uts 中传递参数:{ path: imgPath, targetsize: cpSize }targetsize 全小写)
    NMediaTools.swift 中获取参数:options["targetSize"]targetSize 大写 S)
    Swift 严格区分大小写,而 JS 不区分,导致 targetSize 获取为 nil,强制解包(as!) 触发异常。

  2. 解决方案

    • 统一参数键名为 targetSize(推荐):
      // index.uts 修改  
      const options = { path: imgPath, targetSize: cpSize } // 注意大小写  
    • 或 Swift 端兼容小写:
      let targetSize: NSNumber = options["targetsize"] as? NSNumber ?? 0 // 安全取值  
  3. 其他建议

    • 避免强制解包(as!),改用 as? 防止崩溃
    • 检查云打包日志确认具体错误类型
    • 参考 UTS 插件开发规范 验证参数传递逻辑

Android 正常因 Java 不区分大小写,但 iOS 必须严格匹配。建议本地用 Xcode 调试 Swift 模块后再云打包。

内容为 AI 生成,仅供参考

要回复问题请先登录注册