1_用instancetype替代id的几点原因

本文探讨了在Objective-C中使用instancetype代替id的原因。通过对比分析,指出了使用instancetype在明确性、一致性和良好习惯培养方面的优势。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

======文档更新状态=====
2015-12-16:发布
文章通俗易懂、不想翻译

==================

文章摘自:http://tewha.net/2013/02/why-you-should-use-instancetype-instead-of-id/

Why you should use instancetype instead of id

In my previous entry, I discussed when id will be promoted to instancetype. But now that I’ve explained this, I’d like to explain why you should understand this but not rely on it. Instead, you should use instancetype directly.

Let me start with this bold statement, then I’ll back up and explain it: Use instancetype whenever it’s appropriate, which is whenever a class returns an instance of that same class.

First, some definitions:

@interface Foo:NSObject
- (id)initWithBar:(NSInteger)bar; // initializer
+ (id)fooWithBar:(NSInteger)bar; // convenience constructor
@end
For a convenience constructor, you should always use instancetype. The compiler does not automatically convert id to instancetype.

For initializer, it’s more complicated. When you type this:

  • (id)initWithBar:(NSInteger)bar
    …the compiler will pretend you typed this instead:

  • (instancetype)initWithBar:(NSInteger)bar
    This was necessary for ARC. This is why people will tell you it isn’t necessary to use instancetype, though I contend you should. The rest of this answer deals with this.

There’s three advantages:

Explicit. Your code is doing what it says, rather than something else.
Pattern. You’re building good habits for times it does matter, which do exist.
Consistency. You’ve established some consistency to your code, which makes it more readable.
Explicit

It’s true that there’s no technical benefit to returning instancetype from an init. But this is because the compiler automatically converts the id to instancetype. You are relying on this quirk; while you’re writing that the init returns an id, the compiler is interpreting it as if it returns an instancetype.

These are equivalent to the compiler:

  • (id)initWithBar:(NSInteger)bar;
  • (instancetype)initWithBar:(NSInteger)bar;
    These are not equivalent to your eyes. At best, you will learn to ignore the difference and skim over it. This is not something you should learn to ignore.

Pattern

While there’s no difference with init and other methods, there is a different as soon as you define a convenience constructor.

These two are not equivalent:

  • (id)fooWithBar:(NSInteger)bar;
  • (instancetype)fooWithBar:(NSInteger)bar;
    You want the second form. If you are used to typing instancetype as the return type of a constructor, you’ll get it right every time.

Consistency

Finally, imagine if you put it all together: you want an init function and also a convenience constructor.

If you use id for init, you end up with code like this:

  • (id)initWithBar:(NSInteger)bar;
  • (instancetype)fooWithBar:(NSInteger)bar;
    But if you use instancetype, you get this:

  • (instancetype)initWithBar:(NSInteger)bar;

  • (instancetype)fooWithBar:(NSInteger)bar;
    It’s more consistent and more readable. They return the same thing, and now that’s obvious.

Conclusion

Unless you’re intentionally writing code for old compilers, you should use instancetype when appropriate.

You should hesitate before writing a message that returns id. Ask yourself: Is this returning an instance of this class? If so, it’s an instancetype.

There are certainly cases where you need to return id; namely, if you’re returning a different class. But you’ll probably use instancetype much more frequently than id.

public function importGoodsDetail(): Json { set_time_limit(0); ini_set('memory_limit', '2048M'); $params = $this->request->param(); if (empty($params["file"])) { return $this->fail('请上传要导入的文件',[],0,1); } $titleArr = [ "物流履约单号" => "logistics_order_no", "货品名称" => "product_name", "货品ID" => "product_id", "货品件数" => "quantity", "货品重量(克)" => "weight", "货品长度(毫米)" => "length", "货品宽度(毫米)" => "width", "货品高度(毫米)" => "height", "货品体积(立方毫米)" => "volume", ]; try { // 使用生成器获取数据流 $dataGenerator = $this->importExeclGenerator($params['file']); $batchSize = 3000; // 每批处理300条,可根据服务器性能调整 $model = new GoodsDetailModel(); $total = 0; $success = 0; $failedBatches = []; // 记录失败的批次 // 直接处理数据,要使用事务 $batchData = []; foreach ($dataGenerator as $rowData) { $total++; // 转换数据格式 $item = []; foreach ($rowData as $k => $val) { if ($k && $val && isset($titleArr[$k])) { $item[$titleArr[$k]] = trim($val); } } if (!empty($item)) { $batchData[] = $item; } // 达到批次大小或处理完所有数据时插入数据库 if (count($batchData) >= $batchSize) { if (!empty($batchData)) { try { $model->saveAll($batchData); $success += count($batchData); } catch (\Exception $e) { // 记录失败批次(可选) $failedBatches[] = [ 'startRow' => $total - count($batchData) + 1, 'endRow' => $total, 'error' => $e->getMessage() ]; } } $batchData = []; // 清空批次数据 } } // 处理剩余数据 if (!empty($batchData)) { try { //TODO:有优化的空间 速度可以大大提升 赶进度暂且这样... $model->saveAll($batchData); $success += count($batchData); } catch (\Exception $e) { $failedBatches[] = [ 'startRow' => $total - count($batchData) + 1, 'endRow' => $total, 'error' => $e->getMessage() ]; } } // 构建返回结果 $resultMsg = "导入成功,共{$total}条数据,成功导入{$success}条"; if (!empty($failedBatches)) { $resultMsg .= ",失败 ".$total - $success." 条,具体报错信息:".$e->getMessage(); return $this->fail('导入失败',['remark' => $resultMsg],1,1); } return $this->success('导入成功',['remark' => $resultMsg],1,1); } catch (\Exception $e) { return $this->fail('导入失败',['remark' => $e->getMessage()],1,1); } } 上面这是我的php后端用于导入文件,其中调用导入的方法如下: private function importExeclGenerator(string $file_name): \Generator { try { $path = app()->getRootPath() . "public/" . $file_name; $reader = IOFactory::createReaderForFile($path); $reader->setReadDataOnly(true); $spreadsheet = $reader->load($path); // 获取第一个工作表 $sheet = $spreadsheet->getSheet(0); // 获取表头 $title = $sheet->rangeToArray('A1:' . $sheet->getHighestColumn() . '1', null, true, true, true)[1]; if (empty($title)) { throw new \Exception('Excel表头为空'); } $highestRow = $sheet->getHighestRow(); // 逐行生成数据,一次性加载到内存 for ($row = 2; $row <= $highestRow; $row++) { $rowData = $sheet->rangeToArray( 'A' . $row . ':' . $sheet->getHighestColumn() . $row, null, true, true, true )[$row]; $item = []; foreach ($rowData as $colIndex => $value) { $colName = $title[$colIndex]; if ($colName) { //$item[$colName] = trim($value); $item[$colName] = $value; } } // 使用yield关键字逐行返回数据 yield $item; } } catch (\Exception $e) { throw $e; } } 我是使用了流的形式导入,但是报错502网关超时,我的前端代码如下: //菜鸟仓储费--正向配送费--货品明细导入api export function importGoodsDetail(params:any) { return request.post({ url:'/wareHousFee.newFie.forwardDeliveryFee.GoodsDetail/importGoodsDetail', params, // 我们的标准是100万数据导出 设置超时时间为5小时 (5 * 60 * 60 * 1000 = 18,000,000毫秒) timeout:18000000 }) } <template> <el-dialog :title="upload.title" v-model="show" width="400px"> <el-upload ref="uploadRefs" :limit="1" accept=".xlsx, .xls" :timeout="120000" :max-size="200*1024*1024" :headers="upload.headers" :action="upload.url" :on-progress="handleFileUploadProgress" :on-success="handleFileSuccess" :auto-upload="true" :on-error="uploadErr" drag> <i class="el-icon-upload"></i> <div class="el-upload__text">将文件拖到此处,或点击上传</div> <div class="el-upload__tip text-center" slot="tip"> <span>仅允许导入xls、xlsx格式文件。</span> <!-- <el-link type="primary" :underline="false" style="font-size: 12px; vertical-align: baseline" v-hasPermi="['teacher:import:muban']" @click="importTemplate">下载模板</el-link> --> </div> </el-upload> <div slot="footer" class="dialog-footer"> <el-button type="primary" @click="submitFileForm" :disabled="submitLoading">确 定</el-button> <el-button @click="handleClose">取 消</el-button> </div> </el-dialog> </template> <script lang="ts" setup > import { getToken } from '@/utils/auth' import feedback from '@/utils/feedback' import config from '@/config' import type { ElUpload } from 'element-plus' import { importGoodsDetail } from '@/api/wareHousFee/newBie' import { useRoute } from 'vue-router' const emit = defineEmits(['success']) const route = useRoute() const show = ref(false); const uploadRefs = shallowRef<InstanceType<typeof ElUpload>>() const upload: any = ref({ title: "正向配送费货品明细导入", open: false, url: ref(`${config.baseUrl}${config.urlPrefix}/upload/excel`), headers: { token: getToken(), version: config.version } }); const fileUrl=ref(""); var source= route.query.source; // 控制按钮加载状态 const submitLoading = ref(false); //下载导入模版 未使用 const importTemplate = async () => {} //文件上传进度的回调函数 const handleFileUploadProgress = async (response: any, file: any, fileLists: any[]) => {} //文件上传成功的回调函数 const handleFileSuccess = async (response: any, file: any, fileLists: any[]) => { fileUrl.value=response.data.url } //文件上传失败的回调函数 // 文件上传失败的回调函数 const uploadErr = (error: any, file: any, fileList: any[]) => { console.log('[上传失败] 错误触发'); // 区分同类型的错误 if (error instanceof Error) { console.log('[上传失败] 错误对象:', error); console.log('[上传失败] 错误信息:', error.message); } else { console.log('[上传失败] 响应数据:', error); // 尝试解析可能的HTTP状态码 if (error.response && error.response.status) { console.log('[上传失败] 状态码:', error.response.status); console.log('[上传失败] 状态文本:', error.response.statusText); } } console.log('[上传失败] 文件信息:', { name: file.name, size: `${(file.size / 1024 / 1024).toFixed(2)}MB`, type: file.type }); } //上传组件确定按钮操作 const submitFileForm = async () => { //防止重复点击确定按钮 if (submitLoading.value) return; feedback.loading('正在导入中...') try { //设置确定按钮禁用 submitLoading.value = true; // 调用后端接口并获取返回结果 const response = await importGoodsDetail({ file: fileUrl.value }); feedback.closeLoading() // 控制台打印返回结果(便于调试) console.log("接口返回结果:", response); emit("success"); show.value = false; } catch (error) { // 接口调用异常(如网络错误、超时等) //console.error("接口调用异常:", error); feedback.msgError("导入失败,接口调用异常,请稍后再试"); feedback.closeLoading() } finally { //无论成功或者失败都设置按钮可点击 submitLoading.value = false; feedback.closeLoading() } } //点击弹窗关闭按钮 const handleClose = () => { fileUrl.value = '' show.value = false } //点击按钮除非open事件打开弹窗 const open = (source:any) => { show.value = true } //defineExpose({ open }) 的核心作用是将子组件的 open 方法显式暴露给父组件 defineExpose({ open }) </script> 请帮我具体排查一下接口是是写的有问题啊
07-06
内容概要:本文详细探讨了基于MATLAB/SIMULINK的多载波无线通信系统仿真及性能分析,重点研究了以OFDM为代表的多载波技术。文章首先介绍了OFDM的基本原理和系统组成,随后通过仿真平台分析了同调制方式的抗干扰性能、信道估计算法对系统性能的影响以及同步技术的实现与分析。文中提供了详细的MATLAB代码实现,涵盖OFDM系统的基本仿真、信道估计算法比较、同步算法实现和同调制方式的性能比较。此外,还讨论了信道特征、OFDM关键技术、信道估计、同步技术和系统级仿真架构,并提出了未来的改进方向,如深度学习增强、混合波形设计和硬件加速方案。; 适合人群:具备无线通信基础知识,尤其是对OFDM技术有一定了解的研究人员和技术人员;从事无线通信系统设计与开发的工程师;高校通信工程专业的高年级本科生和研究生。; 使用场景及目标:①理解OFDM系统的工作原理及其在多径信道环境下的性能表现;②掌握MATLAB/SIMULINK在无线通信系统仿真中的应用;③评估同调制方式、信道估计算法和同步算法的优劣;④为实际OFDM系统的设计和优化提供理论依据和技术支持。; 其他说明:本文仅提供了详细的理论分析,还附了大量的MATLAB代码示例,便于读者动手实践。建议读者在学习过程中结合代码进行调试和实验,以加深对OFDM技术的理解。此外,文中还涉及了一些最新的研究方向和技术趋势,如AI增强和毫米波通信,为读者提供了更广阔的视野。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值