当前位置:网站首页>Swift 通用类型AnyObject与Any的使用
Swift 通用类型AnyObject与Any的使用
2022-04-22 02:24:00 【秋秋小事】
//
// AnyObjectType.swift
// SwiftCode
//
// Created by Alisa on 2022/4/12.
// Copyright 2022 Alisa. All rights reserved.
//
import Foundation
/*
在实际开发中,开发者经常会使用一些通用类型。例如在ios的Cocoa框架中,NSObject类是大部分类的基类,在Objective-C语言中,使用id类型来描述通用的对象类型,在某些情况下,使用通用类型将为开发带来很大的方便
在Swift中,通用类型有两种:AnyObject、Any
** AnyObject
AnyObject来作为引用类型的通用类型,它不可以用来描述值的类型。比如不可以用于结构体、枚举等
** Any
Any可以用来描述任意类型,包括值类型和引用类型
*/
//枚举:门店的类型
enum StoreType{
case Food
case Daily
case Other
}
//结构体:门店的牌子
struct Brand{
var color:String
let hasLamp:Bool
let type:StoreType
//展示晚上的灯光效果
func showTheNightStyle(){
print("showTheNightStyle, the color is: \(self.color)")
}
}
class Street{
var stores:Array<AnyObject> //这里使用AnyObject来定义数组中每个元素为引用的通用类型
init() {
self.stores = []
}
deinit{
print("Street Deinit!")
}
}
class FoodStore{
var name:String
init() {
self.name = "Food Store"
}
deinit{
print("FoodStoren Deinit!")
}
}
class DailyExpensesStore{
var name:String
init() {
self.name = "Daily Expenses Store"
}
deinit{
print("DailyExpensesStore Deinit!")
}
}
class AnyObjectType{
//AnyObject通用类型的使用
func useAnyObject(){
let f_store = FoodStore()
let de_store = DailyExpensesStore()
let street = Street()
street.stores.append(f_store)
street.stores.append(de_store)
street.stores.append(de_store)
street.stores.append(f_store)
for storeOne in street.stores{
if storeOne is DailyExpensesStore{
print("current store type is DelayedExecution")
}
if storeOne is FoodStore{
print("current store type is FoodStore")
}
}
/* 打印信息:
current store type is FoodStore
current store type is DelayedExecution
current store type is DelayedExecution
current store type is FoodStore
Street Deinit!
DailyExpensesStore Deinit!
FoodStoren Deinit!
*/
}
//Any通用类型的使用
func useAny(){
let f_store = FoodStore()
let de_store = DailyExpensesStore()
let street = Street()
let brandOne = Brand(color: "yellow", hasLamp: true, type: StoreType.Food)
let brandTwo = Brand(color: "blue", hasLamp: false, type: StoreType.Food)
let typeOne = StoreType.Daily
let typeTwo = StoreType.Other
let things:Array<Any> = [f_store, de_store, street, brandTwo, brandOne, typeOne, typeTwo]
for one in things{
if one is FoodStore{
print("FoodStore!")
}
if one is Street{
print("Street!")
}
if one is Brand{
print("Brand")
let oneThing:Brand = one as! Brand
oneThing.showTheNightStyle()
}
if one is StoreType{
let oneThing:StoreType = one as! StoreType
print("this store type is: \(oneThing)")
}
}
/* 打印信息:
FoodStore!
Street!
Brand
showTheNightStyle, the color is: blue
Brand
showTheNightStyle, the color is: yellow
this store type is: Daily
this store type is: Other
Street Deinit!
DailyExpensesStore Deinit!
FoodStoren Deinit!
*/
}
}
版权声明
本文为[秋秋小事]所创,转载请带上原文链接,感谢
https://blog.csdn.net/jiaoyangdetian/article/details/124330864
边栏推荐
- Review of SV knowledge points
- Pycharm同时执行多个脚本文件
- postgresql中在查询结果中将字符串转换为整形或浮点型
- DEJA_VU3D - Cesium功能集 之 012-军事标绘系列六:自定义多边形
- 高级android面试答案,Gradle源码全解析
- uniapp处理强制刷新问题
- AI application theory - Smart farm (cattle farm without monitoring)
- Page 58 Siemens digital solutions for machine tool industry
- Shell basic syntax
- 68 smart pipe gallery project construction solution
猜你喜欢

【pytorch图像分类】AlexNet网络结构

2022年物联网安全的发展趋势

49 pages enterprise digital transformation cases and common tools enterprise digital transformation ideas

Why is Nacos so strong

高级android面试答案,Gradle源码全解析

Redis cache database uses redis shake for data synchronization

Evaluation of arithmetic four mixed operation expression

ctf-wiki本地搭建记录

Scala installation and environment configuration
Analysis of advanced interview questions, Alibaba released the "limited edition" Android zero foundation dictionary
随机推荐
Golang 1.8 generic testing
error:there‘s no Qt version assigned to project please assign a Qt installation in qt project settin
Why can the Internet industry attract more and more young people? Especially programmers
Swoole high performance in memory database use and configuration tutorial
golang 1.8泛型测试
flutter 不用状态栏的导航栏
13. Installation mode of system software
Page 49 information planning and digital transformation of petroleum and petrochemical industry
语义分割之FCN网络详解 全卷积网络
The shortcut key for modifying the drawing board size of illustrator (AI) is shift + O by default
13.系统软件安装方式
Alibaba P9 explains high concurrency in detail, and shows you how Taobao can resist large-scale second kill activities such as double 11
Why is Nacos so strong
2022 software designer examination knowledge points: linear table
flutter 音乐播放器audioplayer
DEJA_VU3D - Cesium功能集 之 012-军事标绘系列六:自定义多边形
[timing] reformer: local sensitive hash (LSH) to achieve efficient transformer paper notes
How to select the appropriate neo4j Version (2022)
【项目】小帽外卖(七)
Knowledge points of machine learning and deep learning