// 设计一个函数,给出第一个数字、公差、元素个数,返回一个等差数列的数组
func generateArithmeticProgression(startValue: Int, commonDifference: Int, numberOfElements: Int) -> [Int] {
var array = [Int](count: numberOfElements, repeatedValue: 0)
for i in 0..<array.count {
array[i] = startValue + commonDifference * i
}
return array
}
print(generateArithmeticProgression(5, commonDifference: 3, numberOfElements: 10))
// 给出一个数组判断是否为等差数列
func isProgression(array: [Int]) -> Bool {
if array.count > 2 {
let newArray = array.sort(<)
let commonDiff = newArray[1] - newArray[0]
for i in 1..<newArray.count - 1 {
if newArray[i + 1] - newArray[i] != commonDiff {
return false
}
}
}
return true
}
print(isProgression([1, 3, 4, 5, 6]))
print(isProgression([10, 8, 6, 4, 2]))
print(isProgression([300, 100, 200]))
// 设计一个函数给出一个长度,生成一个那么长度的由字母和数字组成的字符串验证码
extension String {
// 对字符串进行扩展
var length: UInt32 {
get { return UInt32(self.characters.count) }
}
subscript(index: Int) -> Character {
get { return self[self.startIndex.advancedBy(index)] }
}
}
func randomInt(min: UInt32, _ max: UInt32) -> Int {
return Int(arc4random_uniform(max - min + 1) + min)
}
func generateVerificationCode(length: Int) -> String {
var code = ""
if length > 0 {
let str = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
for _ in 0..<length {
// code.append(str[randomInt(0, UInt32(str.characters.count) - 1)])
code.append(str[randomInt(0, str.length - 1)])
//code.append(str[str.startIndex.advancedBy(randomInt(0, UInt32(str.characters.count) - 1))])
}
}
return code
}
for _ in 1...10 {
print(generateVerificationCode(4))
}
// 设计一个学生找枪手考试的场景
// 代理模式
protocol ExamCandidate: class {
func answerTheQuestion()
}
class LazyStudent: ExamCandidate {
var name: String
init(name: String) {
self.name = name
}
func answerTheQuestion() {
}
}
class Gunman: ExamCandidate {
var name: String
var target: LazyStudent?
init(name: String) {
self.name = name
}
func answerTheQuestion() {
if let stu = target {
print("姓名: \(stu.name)")
print("奋笔疾书答案")
print("提交试卷")
}
}
}
let stu = LazyStudent(name: "王大锤")
let gun = Gunman(name: "骆昊")
gun.target = stu
gun.answerTheQuestion()
// 委托回调模式
protocol ExamDelegate: class {
func answerTheQuestion()
}
class LazyStudent {
var name: String
weak var delegate: ExamDelegate?
init(name: String) {
self.name = name
}
func joinExam() {
print("姓名: \(name)")
delegate?.answerTheQuestion()
}
}
class Gunman: ExamDelegate {
func answerTheQuestion() {
print("奋笔疾书各种答案")
}
}
let stu = LazyStudent(name: "王大锤")
let gun = Gunman()
stu.delegate = gun
stu.joinExam()
// 设计一个员工类和部门类,写上他们各自的属性和方法
enum Gender {
case Male
case Female
}
class Employee {
var id: String
var name: String
var gender: Gender
var tel: String?
weak var dept: Department?
init(id: String, name: String, gender: Gender) {
self.id = id
self.name = name
self.gender = gender
}
func work() {
print("工号为\(id)的\(name)正在工作.")
}
func haveRest() {
print("\(name)正在休息.")
}
}
class Department {
var id: String
var name: String
var empsArray: [Employee] = []
var numberOfEmps: Int {
get { return empsArray.count }
}
init(id: String, name: String) {
self.id = id
self.name = name
}
func addEmp(emp: Employee) {
empsArray.append(emp)
}
func removeEmp(id: String) -> Bool {
if let index = empsArray.indexOf({ $0.id == id }) {
empsArray.removeAtIndex(index)
return true
}
return false
}
}
第四周Swift考试答案 委托回调和代理区别
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 目的: A页面push到B页面,点击B页面的按钮传值到A页面,分别用delegate和Block来实现 1.在页面...
- 题目:The string "PAYPALISHIRING" is written in a zigzag pat...