0
1 2 3 4 5 6 7 8 9 10
if section == 0 {
return "Item(s) in your cart"
} else if section == 1 {
return String("Total tax")
} else if section == 2{
return String("Shipped to")
} else if section == 3 {
return String("Please select a payment method")
}
return nil
Ладно, допустим человек не знает, что такое switch или хранение данных в массиве.
Но зачем явно вызывать конструктор строки? Что, кто-то не поймёт, что это строка?
gorsash ,
30.05.2016 (Updated 30.03.2018 )
0
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
import Foundation
class UserAddress {
private var ID = Int32()
private var stateID = Int32()
private var eavObjectID = Int32()
private var firstName = String()
private var lastName = String()
private var companyName = String()
private var address = String()
private var address2 = String()
private var city = String()
private var stateName = String()
private var postalCode = String()
private var countryID = String()
private var phone = String()
func setID(ID: Int32) { self.ID = ID }
func setStateID(stateID: Int32) { self.stateID = stateID }
func setEavObject(eavObjectID: Int32) { self.eavObjectID = eavObjectID }
func setFirstName(firstName: String) { self.firstName = firstName }
func setLastName(lastName: String) { self.lastName = lastName }
func setCompanyName(companyName: String) { self.companyName = companyName }
func setAddress(address: String) { self.address = address }
func setAddressTwo(address2: String) { self.address2 = address2 }
func setCity(city: String) { self.city = city }
func setStateName(stateName: String) { self.stateName = stateName }
func setPostalCode(postalCode: String) { self.postalCode = postalCode }
func setCountryID(countryID: String) { self.countryID = countryID }
func setPhone(phone: String) { self.phone = phone }
func getID() -> Int32 { return self.ID }
func getStateID() -> Int32 { return self.stateID }
func getEavObject() -> Int32 { return self.eavObjectID }
func getFirstName() -> String { return self.firstName }
func getLastName() -> String { return self.lastName }
func getCompanyName() -> String { return self.companyName }
func getAddres() -> String { return self.address }
func getAddressTwo() -> String { return self.address2 }
func getCity() -> String { return self.city }
func getStateName() -> String { return self.stateName }
func getPostalCode() -> String { return self.postalCode }
func getCountryID() -> String { return self.countryID }
func getPhone() -> String { return self.phone }
}
Java головного мозга. Писал android девелопер
gorsash ,
06.05.2016 (Updated 29.03.2018 )
0
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
if enabled {
billingNameTextField.textColor = UIColor.blackColor()
billingLasNameTextField.textColor = UIColor.blackColor()
billingCompanyNameTextField.textColor = UIColor.blackColor()
billingPhoneNumberTextField.textColor = UIColor.blackColor()
billingAddressTextField.textColor = UIColor.blackColor()
billingAddressTwoTextField.textColor = UIColor.blackColor()
billingCityTextField.textColor = UIColor.blackColor()
billingStateTextField.textColor = UIColor.blackColor()
billingStateTextField.textColor = UIColor.blackColor()
billingPostalCodeTextField.textColor = UIColor.blackColor()
billingCountryTextField.textColor = UIColor.blackColor()
} else {
billingNameTextField.textColor = UIColor.lightGrayColor()
billingLasNameTextField.textColor = UIColor.lightGrayColor()
billingCompanyNameTextField.textColor = UIColor.lightGrayColor()
billingPhoneNumberTextField.textColor = UIColor.lightGrayColor()
billingAddressTextField.textColor = UIColor.lightGrayColor()
billingAddressTwoTextField.textColor = UIColor.lightGrayColor()
billingCityTextField.textColor = UIColor.lightGrayColor()
billingStateTextField.textColor = UIColor.lightGrayColor()
billingStateTextField.textColor = UIColor.lightGrayColor()
billingPostalCodeTextField.textColor = UIColor.lightGrayColor()
billingCountryTextField.textColor = UIColor.lightGrayColor()
}
Зачем использовать промежуточные переменные, когда можно написать такой большой if
gorsash ,
05.05.2016 (Updated 29.03.2018 )
0
1 2 3 4
let serviceKeys = Array(Set(listOfRestrictions.map {$0.service!})).map{ $0 }
serviceKeys.map { key in
self.restriction[key] = listOfRestrictions.filter{ $0.service! == key }.map { $0.sample! }
}
По-моему, количество wtf на три строчки кода зашкаливает
tapoton ,
16.02.2016 (Updated 29.03.2018 )
0
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
public var messageStatus = MessageStatus.IncomingMessage {
didSet {
self.messageStatusChanged(messageStatus)
switch messageStatus {
case .IncomingMessage:
haloView.alpha = 0.0
whiteCircleView.fillColor = .clearColor()
whiteCircleView.borderColor = .clearColor()
colorCircleView.fillColor = .clearColor()
colorCircleView.borderColor = .clearColor()
haloView.color = .clearColor()
default:
whiteCircleView.fillColor = collectionViewBackgroundColor
haloView.alpha = 1.0
}
switch messageStatus {
case .ReadedMessage:
colorCircleView.fillColor = bubbleColor
colorCircleView.borderColor = bubbleColor
haloView.color = bubbleColor
case .DeliveredMessage:
colorCircleView.fillColor = .whiteColor()
colorCircleView.borderColor = bubbleColor
haloView.color = bubbleColor
case .NonDeliveredMessage:
colorCircleView.fillColor = .clearColor()
colorCircleView.borderColor = bubbleColor
haloView.color = .clearColor()
default: break
}
}
}
Когда одного switch мало...
PS: язык Swift
TeknoMatik ,
07.02.2016 (Updated 29.03.2018 )
0
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
//
// How ARC causes memory leaks and leads to crashes out of the blue
//
import Foundation
let noLeak = 131030 // Empirically found constant
let withLeak = noLeak*10
// Single-linked list
class R {
var _a : R?
init(a : R?) {
_a = a
}
// One have to resort to a manual C-like code to fix it
//
// deinit {
// var i = self._a;
// while let i2 = i {
// let t = i2._a
// i2._a = nil
// i = t
// }
// }
}
func test(n:Int, leak:Bool) {
let p0 = R(a : nil)
var p = R(a : p0)
for _ in 1...n {
p = R(a : p)
}
if leak {
// When the list is not cyclic it will be deleted by ARC just fine...
p0._a = p
}
} // Oh wait, the destructor isn't tail-recursive...
test(withLeak, leak: true)
print("Bad leaking function")
test(noLeak, leak: false)
print("Good function")
гц -- сила, ARC -- могила
(язык -- Swift, если что)
CHayT ,
28.11.2015 (Updated 29.03.2018 )
0
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
struct Family: Enumerable {
var name = "Smith"
var father = "Bob"
var mother = "Alice"
var child = "Carol"
func each(block: (String) -> Void) {
for i in 0...2 {
switch i {
case 0: block("\(father) \(name)")
case 1: block("\(mother) \(name)")
case 2: block("\(child) \(name)")
default: break
}
}
}
}
http://matthijshollemans.com/2015/07/22/mixins-and-traits-in-swift-2/
Pattern matching головного мозга. Верно говорят, что тот, кто не умеет делать, идет учить других как надо делать.
gumbert ,
21.09.2015 (Updated 29.03.2018 )
0
1 2 3 4 5 6 7
@objc(Squirrel)
class Белка {
@objc(initWithName:)
init (имя: String) { /*...*/ }
@objc(hideNuts:inTree:)
func прячьОрехи(Int, вДереве: Дерево) { /*...*/ }
}
swift по-русски
https://developer.apple.com/library/prerelease/ios/documentation/swift/conceptual/buildingcocoaapps/interactingwithobjective-capis.html
absolut ,
10.06.2014 (Updated 27.03.2018 )
0
1
Ждем нового раздела под язык Swift. Ожидается наплыв.
https://developer.apple.com/swift/
http://habrastorage.org/getpro/habr/comment_images/f80/9bd/f07/f809bdf079e06818109355db44e9430b.png
http://habrastorage.org/getpro/habr/comment_images/45a/feb/cfe/45afebcfe01065e7bdb2b618ea045f18.png
http://habrastorage.org/getpro/habr/comment_images/32e/c47/ae5/32ec47ae5be2bb4f540e318764c8f2ab.png
http://habrastorage.org/getpro/habr/comment_images/d21/480/e59/d21480e59827fc1c6b93150c91fdcf90.png
http://habrastorage.org/getpro/habr/comment_images/b30/513/b4f/b30513b4f3345b51b18565a235b6ab6a.png
LispGovno ,
03.06.2014 (Updated 27.03.2018 )