12、Swift 中可用类型的理解与应用

Swift 中可用类型的理解与应用

1. 理解值类型

在 Swift 里,值类型有着独特的特性。当你在控制台看到错误提示,说 pet 是 let 常量,不允许更改其名称时,可通过如下方式将 pet 转为变量:

func printName(for pet: Pet) {
    var pet = pet
    print(pet.name)
    pet.name = "Jesse"
}

运行 playground 后,仔细查看控制台会发现,第二次打印时 pet 的名称并未改变。这体现了值类型的一个关键特性:传递的是对象的副本,而非内存地址的引用。这就解释了为何不能更改赋值给常量的值类型的属性,因为更改属性会改变值类型的值,进而改变常量的值。当把 dog 传递给 printName 函数时,传递的是 Pet 实例的副本,所以对该实例所做的任何更改仅在 printName 函数内部有效,不会应用到 dog 上。

值类型的这种行为使其使用起来极为安全,因为其他对象或函数很难对值类型进行意外更改。而且,若将某个东西定义为常量,它就真的是常量。值类型的另一个特点是通常速度快且轻量级,因为它们可以存在于栈上,而引用类型存在于堆上。

2. 理解结构体(Structs)

结构体在定义

相关推荐

3、Swift编程基础:简单值与类型入门

本文介绍了Swift编程的基础知识,包括Swift Playgrounds的创建与使用、常见的数据类型(如整数、浮点数、字符串和布尔值)、常量与变量的声明和使用、类型推断与类型安全机制,以及运算符的基本用法。通过实践示例和流程图,帮助初学者快速入门Swift语言,为进一步开发iOS应用打下坚实基础。文中还提供了环境搭建、代码实践和学习建议,适合编程新手系统学习Swift语言的核心概念与应用。

ios99的博客 53

24、深入探索Go与Swift编程语言

本文深入探讨了Go和Swift编程语言的核心特性、应用场景及优势。Go以其高效的并发模型和强大的性能,广泛应用于服务器端和分布式系统;而Swift则作为苹果平台的首选语言,强调安全性和简洁性,适用于iOS、macOS等原生应用开发。通过对比两者的语言设计和实际用例,帮助开发者更好地理解和选择适合自身项目需求的语言。

ll5678的博客 165

Swift编程进阶:深入理解可选类型与间接拆包

Swift作为Apple公司推出的一种现代编程语言,自2014年问世以来迅速成为了iOS和macOS应用开发的主流语言。Swift以其简洁的语法、强大的性能和安全特性,吸引了全球数百万的开发者。在本章节中,我们将探讨Swift的基本概念、核心特性以及它如何成为开发者优先选择的技术之一。Swift的语法设计旨在提高编程效率和可读性,它的表达式和函数式编程特性使得开发者能够写出更加简洁和表达力强的代码。Swift的强类型系统和编译时安全检查机制则为应用的稳定性提供了保障,有助于避免运行时错误。

weixin_35794316的博客 1011

Xcode 12.0与SDK:全面支持iOS 12及最新技术特性

Xcode 12.0作为苹果公司为开发者提供的最新集成开发环境(IDE),是开发iOS、macOS、watchOS以及tvOS应用不可或缺的工具。自Xcode 12.0发布以来,它带来了诸多改进和新特性,旨在提升开发者的编码体验、增强应用的性能以及简化应用调试流程。在这一章节中,我们将探究Xcode 12.0的核心功能以及它如何协助开发者高效地构建、测试和优化应用程序。我们会从界面布局的变化讲起,深入理解新的编码工具和编辑器特性,以及如何利用Xcode 12.0的新功能提高工作效率。

weixin_34718952的博客 1315

自定义TabBar教程:Swift & iOS12适配

Swift是苹果公司设计的一款编程语言,专为iOS、macOS、watchOS和tvOS应用开发而生。它与Objective-C兼容,并采用了先进的编程模式,使得开发更加高效、安全。在本章节中,我们将深入了解自定义TabBar的实现过程。TabBar是iOS应用中不可或缺的组件,它为用户提供了一个简单直观的方式在应用的不同部分之间切换。尽管UIKit提供了UITabBar类,但在某些特定场景下,我们可能需要创建一个自定义的TabBar以满足设计需求或增强用户体验。

weixin_31185473的博客 559

平均电流控制模式BUCK变换器的环路设计

引言 电压模式的BUCK变换器存在的常见缺点: 1、在没有前馈补偿的时候,对输入源的跳动响应较慢; 2、在不加入电感电流控制的时候,由于不直接对电感电流进行采样,因此不能实时的控制电感电流,有可能在某些负载极端变化时,系统直接奔溃; 3、LC组成的二阶系统存在谐振点,谐振峰随负载变化而变化,不容易做到完全消除; 峰值电流模式的BUCK变换器存在的常见缺点: 1、占空比大于50%时,存在次谐波震荡的问题,需要添加斜坡补偿已解决该问题; 2、抗干扰能力较差; 3、峰值电流和平均电流间存在误差,不适合做PFC线路

c007106333的博客 1万+

基于Python的信用卡欺诈检测机器学习案例报告

当今时代,在线支付已成为一种高效便捷的支付方式,我们可以突破时间和空间的限制,输入我们的信用卡各项信息就可以完成在线支付。但随之而来的是这种支付方式也具备一定的风险性,我们无法辨别对方是否是恶意套取我们资金的非法分子。当信用卡信息数据泄露导致金钱失窃并最终失去客户忠诚度和公司声誉时,组织、消费者、银行和商家都会面临风险。2017 年,未经授权的信用卡操作达到了惊人的 1670 万受害者。此外,据美国联邦贸易委员会 (FTC) 报告,2017 年信用卡欺诈索赔数量比上一年高出 40%。

weixin_45673389的博客 6696

GNSS接收机性能测试全攻略:从实验室到真实场景

本文系统介绍了GNSS接收机从实验室到真实场景的完整性能测试流程。首先通过GNSS模拟器进行射频传导测试,评估接收机的基础性能;其次进行天线OTA测试,考察整机系统表现;最后通过精心设计的外场路测,在复杂环境中验证其实际定位精度与可靠性。文章强调,只有结合可控的实验室验证与多变的外场挑战,才能对接收机性能做出全面、真实的评价。

weixin_28365523的博客 519

【HAL库】STM32CubeMX开发----STM32F407----ETH+LAN8720A+LWIP----ping通

本次实验以芯片为MCU,使用25MHz外部时钟源。以太网PHY层芯片为LAN8720A,由MCU引脚PA8提供时钟。

MQ0522的博客 4846

博途S7-1500T 使用工艺对象驱动液压轴(含SimaHydTO库)

利用工艺对象控制液压轴位置含PLC控制和仿真程序文档资料下载地址如下:液压比例伺服阀的相关应用知识请参看下面的博客文章:液压位置闭环控制(比例伺服阀应用)_RXXW_Dor的博客-优快云博客。

RXXW_Dor的博客 2901

Vue3 + 高德地图实现车辆电子围栏监控与报警

通过本文的实现,我们完成了一个通用的车辆电子围栏监控功能:支持多种围栏类型。支持进入/离开/双向报警。使用缓存机制避免重复告警。可与Vue3 + 高德地图无缝集成。在实际项目中,可以结合WebSocket 实时定位,实现车辆的实时监控 + 围栏告警系统🚀。

专注于前端开发与实战技巧,记录从零到一的学习与项目经验。分享 Vue3 / JavaScript / TypeScript / Flutter /Uniapp等前端技术,以及 性能优化、工程化、最佳实践。 1036

(秋招复习)一些SLAM基础

秋招复习--SLAM基础知识篇

weixin_45432823的博客 769

Modbus协议介绍及解析

Modbus协议介绍及解析 1.Modbus简介 Modbus是一种广泛应用于工业控制领域串行通信协议,以其开放性、高可靠性、高效简单性、免费等优点,成为了工业领域通信协议的业界标准,是工业现场电子设备之间常用的连接方式。Modbus按其格式可分为Modbus-RTU,Modbus-ASCII,Modbus-TCP,其中前两者适用于串行通信控制网络中,例如RS485,RS232等,而Modbus-TCP主要应用于基于以太网TCP/IP通信的控制网络中。通过此协议,控制器相互之间、或控制器和其它设备之间进行通

逍遥子的博客 1万+

efm32 入门.pdf

本PDF共计22页,介绍的是操作驱动API,下载与否自己斟酌。

蓝牙模块HM-10手册

蓝牙模块HM-10手册,该手册对蓝牙模块HM-10进行了详细的介绍和使用方法以及一些AT指令。

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

上一篇: 19、双向纹理函数(BTF)建模方法详解
下一篇: 56、团队的活力与可持续发展之道
java5
博客等级 码龄22年 85粉丝 2103原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值