当前位置: 首页 > news >正文

Xcode 项目内 OC 混编 Python,调用 Python 函数,并获取返回值(基于 python 的 c函数库)

1:新建 Xcode 工程
2:工程添加 Python.framework


1597052861430.jpg

3:在当前工程下新建一个名字为 googleT 的 python 文件(googleT.py)


1597052584962.jpg

在 googleT.py 文件内写入一个测试 python 函数


def lgf_translate( str ):var1 = ' Hello World!'print (str + var1)return (str + var1)

4:新建一个 OC 类 GoogleTranslate 名字随意
直接上代码
.h

//
//  GoogleTranslate.h
//  LGFFY
//
//  Created by lai on 2020/7/23.
//  Copyright © 2020 victor. All rights reserved.
//#import <Foundation/Foundation.h>NS_ASSUME_NONNULL_BEGIN@interface GoogleTranslate : NSObject
- (NSString *)pyCallWithModule:(char *)module funcKey:(char *)funcKey Args:(char *)args;
@endNS_ASSUME_NONNULL_END

.m

//
//  GoogleTranslate.m
//  LGFFY
//
//  Created by lai on 2020/7/23.
//  Copyright © 2020 victor. All rights reserved.
//#import "GoogleTranslate.h"
#import <Python/Python.h>@interface GoogleTranslate()
@end
@implementation GoogleTranslate/**OC 调用 Python (基于 c/c++ 调用 Python)@param module python 模块名称@param funcKey 函数名称@param args 函数参数@return 返回值*/
- (NSString *)pyCallWithModule:(char * _Nonnull)module funcKey:(char * _Nonnull)funcKey Args:(char * _Nonnull)args {// 初始化 python 解释器Py_Initialize();if (!Py_IsInitialized()) {return @"error: 初始化环境失败";}// 新增 python 路径这里的新增路径是 xcode 内 python 文件的路径, 这一步很关键, 不设置 python 环境将无法检测到 xcode 工程下的 python 模块PySys_SetPath((char *)[[NSString stringWithFormat:@"%s:%@", Py_GetPath(), [[NSBundle mainBundle] resourcePath]] UTF8String]);// 初始化参数PyObject *pModule = NULL, *pFunc = NULL, *pDict = NULL, *pValue = NULL, *pResult = NULL;pModule = PyImport_ImportModule(module);if (!pModule) {finalize(pModule ,pFunc ,pDict ,pValue, pResult);return [NSString stringWithFormat:@"error: %s 模块找不到", module];}// 使用PyObject* pDict来存储导入模块中的方法字典pDict = PyModule_GetDict(pModule);if(!pDict) {finalize(pModule ,pFunc ,pDict ,pValue, pResult);return [NSString stringWithFormat:@"error: %s 模块中未定义方法", module];}// 获取模块中的函数pFunc = PyDict_GetItemString(pDict, funcKey);if(!pFunc || !PyCallable_Check(pFunc)) {finalize(pModule ,pFunc ,pDict ,pValue, pResult);return @"error: 方法获取失败";}// 函数参数pValue = Py_BuildValue("(z)", args);if(!pValue) {finalize(pModule ,pFunc ,pDict ,pValue, pResult);return @"error: 参数转换失败";}// 调用函数获取 return 值pResult = PyObject_CallObject(pFunc, pValue);if (!pResult) {finalize(pModule ,pFunc ,pDict ,pValue, pResult);return @"error: return 值出错";}//结果处理if (PyString_Check(pResult)) {finalize(pModule ,pFunc ,pDict ,pValue, pResult);return [NSString stringWithUTF8String: PyString_AsString(pResult)];} else if (PyInt_Check(pResult)) {finalize(pModule ,pFunc ,pDict ,pValue, pResult);return [NSString stringWithFormat:@"%ld", PyInt_AsLong(pResult)];} else {NSDictionary *paramsDic = @{@"success": @"yes", @"msg": @"Python 函数 return 值获取失败"};NSError *dataError = nil;NSData *data = [NSJSONSerialization dataWithJSONObject:paramsDic options:NSJSONWritingPrettyPrinted error:&dataError];NSString *paramsStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];finalize(pModule ,pFunc ,pDict ,pValue, pResult);return paramsStr;}}void finalize(PyObject *pModule, PyObject *pFunc, PyObject *pDict, PyObject *pValue, PyObject *pResult) {PyErr_Print();//释放对象if (Py_IsInitialized()) {Py_Finalize();}
}@end

5:在 ViewController 中调用


1597053042535.jpg
GoogleTranslate *translate = [[GoogleTranslate alloc] init];NSString *res = [translate pyCallWithModule:"googleT" funcKey:"lgf_translate" Args:"fffffff"];NSLog(@"%@", res);

6: 输出:


1597053102385.jpg

这只是一个小模型,python 的开源库有很多好的模块,以后也可以辅助 xcode 项目,是不是很方便



喜欢的朋友记得点赞、收藏、关注哦!!!

http://www.lryc.cn/news/486235.html

相关文章:

  • 每日计划-1117
  • 如何用GPT-4o解读视频
  • [ACTF2020]Upload 1--详细解析
  • 【微软:多模态基础模型】(3)视觉生成
  • 整合Druid
  • 基于Python空气质量可视化及预测
  • 第1章-PostgreSQL(PG)介绍
  • moduo之阻塞队列BlockingQueue和BoundedBlockingQueue
  • 大模型Tuning方法详解
  • 爬虫策略与反爬机制——爬虫常见策略
  • Linux基础(十七)——Linux 帐号管理与 ACL 权限设置
  • 【HarmonyOS】鸿蒙系统在租房项目中的项目实战(二)
  • 11.16 Vue element
  • Gin 框架中的路由
  • 在MATLAB中实现自适应滤波算法
  • linux文件与重定向
  • 基于Python的仓库管理系统设计与实现
  • 【Pikachu】URL重定向实战
  • C语言实现3D动态爱心图形的绘制与动画效果
  • 深入理解Nginx:从基础配置到高级优化
  • ONLYOFFICE8.2版本测评,团队协作的办公软件
  • spring 和 grpc 的整合
  • 企业项目级IDEA设置类注释、方法注释模板(仅增加@author和@date)
  • 1 设计模式原则之开闭原则
  • 前端大环境
  • Electron: 主进程和渲染进程之间通信
  • 社交电商的优势及其与 AI 智能名片小程序、S2B2C 商城系统的融合发展
  • 蓝桥杯c++算法学习【4】之简单数论(阶乘约数、求值、循环小数、等差数列、最大比例:::非常典型的必刷例题!!!)
  • 重构代码之删除对参数的赋值
  • Docker的基本概念、安装步骤以及一些简单的用法