1. Swift / Говнокод #25705

    +1

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    /* Create a JSON object from JSON data stream. The stream should be opened and configured. All other behavior of this method is the same as the JSONObjectWithData:options:error: method.
         */
    open class func jsonObject(with stream: InputStream, options opt: ReadingOptions = []) throws -> Any {
        var data = Data()
        guard stream.streamStatus == .open || stream.streamStatus == .reading else {
             fatalError("Stream is not available for reading")
         }
         repeat {
             var buffer = [UInt8](repeating: 0, count: 1024)
             var bytesRead: Int = 0
             bytesRead = stream.read(&buffer, maxLength: buffer.count)
             if bytesRead < 0 {
                 throw stream.streamError!
             } else {
                 data.append(&buffer, count: bytesRead)
             }
         } while stream.hasBytesAvailable
         return try jsonObject(with: data, options: opt)
    }

    Потоковое чтение JSON от авторов "iСделаль"

    Desktop, 07 Июля 2019

    Комментарии (21)
  2. Swift / Говнокод #25669

    +1

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    20. 20
    21. 21
    22. 22
    23. 23
    24. 24
    25. 25
    26. 26
    27. 27
    28. 28
    29. 29
    30. 30
    31. 31
    32. 32
    33. 33
    34. 34
    35. 35
    36. 36
    37. 37
    38. 38
    39. 39
    40. 40
    41. 41
    42. 42
    43. 43
    44. 44
    45. 45
    46. 46
    47. 47
    48. 48
    49. 49
    50. 50
    51. 51
    52. 52
    53. 53
    54. 54
    55. 55
    56. 56
    57. 57
    58. 58
    59. 59
    60. 60
    import SwiftUI
    
    
    struct LandmarkDetail: View {
        @EnvironmentObject var userData: UserData
        var landmark: Landmark
    
    
        var landmarkIndex: Int {
            userData.landmarks.firstIndex(where: { $0.id == landmark.id })!
        }
    
    
        var body: some View {
            VStack {
                MapView(landmark: landmark)
                    .frame(height: 300)
    
    
                CircleImage(image: landmark.image(forSize: 250))
                    .offset(y: -130)
                    .padding(.bottom, -130)
    
    
                VStack(alignment: .leading) {
                    HStack {
                        Text(landmark.name)
                            .font(.title)
    
    
                        Button(action: {
                            self.userData.landmarks[self.landmarkIndex].isFavorite.toggle()
                        }) {
                            if self.userData.landmarks[self.landmarkIndex].isFavorite {
                                Image(systemName: "star.fill")
                                    .foregroundColor(Color.yellow)
                            } else {
                                Image(systemName: "star")
                                    .foregroundColor(Color.gray)
                            }
                        }
                    }
    
    
                    HStack(alignment: .top) {
                        Text(landmark.park)
                            .font(caption)
                        Spacer()
                        Text(landmark.state)
                            .font(.caption)
                    }
                }
                .padding()
    
    
                Spacer()
            }
            .navigationBarTitle(Text(landmark.name), displayMode: .inline)
        }
    }

    https://developer.apple.com/tutorials/swiftui/handling-user-input

    Принципиально новый нескучный "декларативный" UI от компании Apple. В наличии:
    * магические константы
    * спагетти из замыканий
    * биндинги, страшные как атомная война
    * где-то внутри модная хипстерская реактивная либа

    На фоне этого кошмара qml кажется вершиной инженерной мысли

    Desktop, 08 Июня 2019

    Комментарии (40)
  3. Swift / Говнокод #25197

    0

    1. 1
    2. 2
    3. 3
    4. 4
    5. 5
    6. 6
    scheduler.queue.async { [weak self] in
                guard let weakself = self else {
                    return
                }
                
                let locations = weakself.interactor

    kyzmitch, 16 Декабря 2018

    Комментарии (198)
  4. Swift / Говнокод #24885

    0

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    20. 20
    21. 21
    22. 22
    23. 23
    24. 24
    25. 25
    26. 26
    27. 27
    28. 28
    29. 29
    30. 30
    31. 31
    32. 32
    enum ErrorViewHelper {
    
        static var errorIsShown = false
        static var errorView: ErrorView?
    
        private static var errorWindow: UIWindow = {
            let width = (UIApplication.shared.delegate as? AppDelegate)?.window?.frame.size.width ?? 300
            let window = UIWindow(frame: CGRect(x: 0, y: 0, width: width, height: 300))
            return window
        }()
    
        static func show(error: Error) {
            if let error = error as? RequestError {
                switch error {
                case .error(description: let text):
                    showErrorView(error: text)
                case .errorWithMeta(description: let text, meta: _):
                    showErrorView(error: text)
                case .needAuthError:
                    showErrorAlert(title: nil, message: R.string.localizable.needAuthError()) {
                        // TODO: remove router from this class
                        AuthRouterImpl().showAuthPage()
                        AuthRouterImpl().showAuthFlowModally()
                    }
                case .badResponse:
                    showErrorView(error: R.string.localizable.badResponse())
                default: break
                }
            } else {
                showErrorView(error: error.text)
            }
        }

    vkasci, 09 Октября 2018

    Комментарии (20)
  5. Swift / Говнокод #24592

    +1

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    20. 20
    21. 21
    22. 22
    23. 23
    24. 24
    25. 25
    26. 26
    27. 27
    28. 28
    29. 29
    30. 30
    31. 31
    32. 32
    // NextViewController.swift
    
    override func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) {
        NSUserDefaults.standardUserDefaults().setInteger(indexPath.row, forKey: "Selected offense")
    		
        let offense: NSDictionary = self.offenses.objectAtIndex(indexPath.row) as NSDictionary
        let id: Int = offense.objectForKey("id") as Int
        let title: String = offense.objectForKey("title") as String
        NSUserDefaults.standardUserDefaults().setInteger(id, forKey: "Selected offense id")
        NSUserDefaults.standardUserDefaults().setObject(title, forKey: "Selected offense title")
    }
    
    // PreviousViewController.swift
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        hideKeyboard()
        tableView.reloadData()
    }
    	
    func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
        let kCellIndetifier: String = "NewOffenseCell"
        var cell: UITableViewCell = tableView.dequeueReusableCellWithIdentifier(kCellIndetifier, forIndexPath: indexPath) as UITableViewCell
    		
        if cell == nil {
            cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: kCellIndetifier)
        }
    		
        cell.text = NSUserDefaults.standardUserDefaults().stringForKey("Selected offense title")
        cell.font = UIFont.systemFontOfSize(20)
        return cell
    }

    Реализуем колбэки *лицорука*

    def, 06 Августа 2018

    Комментарии (14)
  6. Swift / Говнокод #24591

    0

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    20. 20
    21. 21
    22. 22
    23. 23
    24. 24
    25. 25
    26. 26
    27. 27
    28. 28
    29. 29
    30. 30
    31. 31
    32. 32
    33. 33
    34. 34
    35. 35
    36. 36
    37. 37
    38. 38
    func createCrossForRemovingImage() {
    		var firstCrossView: UIImageView = UIImageView(frame: CGRectMake(90, -10, 30, 30))
    		firstCrossView.layer.cornerRadius = 15
    		firstCrossView.image = UIImage(named: "Cross")
    		firstCrossView.tag = 200
    		firstCrossView.userInteractionEnabled = true
    		firstCrossView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: "removeImage:"))
    		firstCrossView.hidden = true
    		
    		var secondCrossView: UIImageView = UIImageView(frame: CGRectMake(90, -10, 30, 30))
    		secondCrossView.layer.cornerRadius = 15
    		secondCrossView.image = UIImage(named: "Cross")
    		secondCrossView.tag = 200
    		secondCrossView.userInteractionEnabled = true
    		secondCrossView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: "removeImage:"))
    		secondCrossView.hidden = true
    		
    		var thirdCrossView: UIImageView = UIImageView(frame: CGRectMake(90, -10, 30, 30))
    		thirdCrossView.layer.cornerRadius = 15
    		thirdCrossView.image = UIImage(named: "Cross")
    		thirdCrossView.tag = 200
    		thirdCrossView.userInteractionEnabled = true
    		thirdCrossView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: "removeImage:"))
    		thirdCrossView.hidden = true
    		
    		var fourthCrossView: UIImageView = UIImageView(frame: CGRectMake(90, -10, 30, 30))
    		fourthCrossView.layer.cornerRadius = 15
    		fourthCrossView.image = UIImage(named: "Cross")
    		fourthCrossView.tag = 200
    		fourthCrossView.userInteractionEnabled = true
    		fourthCrossView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: "removeImage:"))
    		fourthCrossView.hidden = true
    		
    		firstImageView.addSubview(firstCrossView)
    		secondImageView.addSubview(secondCrossView)
    		thirdImageView.addSubview(thirdCrossView)
    		fourthImageView.addSubview(fourthCrossView)
    	}

    Когда-то я это написал Оо

    def, 06 Августа 2018

    Комментарии (18)
  7. Swift / Говнокод #24489

    0

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    type file;
    
    app (file o) simulation ()
    {
      simulate stdout=filename(o);
    }
    
    foreach i in [0:9] {
      file f <single_file_mapper; file=strcat("output/sim_",i,".out")>;
      f = simulation();
    }

    Более другой swift

    Desktop, 13 Июля 2018

    Комментарии (39)
  8. Swift / Говнокод #24431

    +1

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    @dynamicMemberLookup
    struct Uppercaser {
        subscript(dynamicMember input: String) -> String {
            return input.uppercased()
        }
    }
    
    Uppercaser().hello // → "HELLO"
    // You can type anything, as long as Swift accepts it as an identifier.
    Uppercaser().käsesoße // → "KÄSESOSSE"

    https://oleb.net/blog/2018/06/dynamic-member-lookup

    The proposal and implementation of dynamic member lookup was largely driven by the Swift for TensorFlow team at Google. Their main motivation is to facilitate interoperability between Swift and dynamic languages, specifically (though not exclusively) Python. Their goal is to make it possible to call Python code from Swift with a pretty and familiar syntax.

    We need MOAR syntax sugar

    Desktop, 30 Июня 2018

    Комментарии (18)
  9. Swift / Говнокод #24322

    −2

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    20. 20
    21. 21
    22. 22
    23. 23
    24. 24
    25. 25
    26. 26
    static func == (lhs: Card, rhs: Card) -> Bool {
           return (lhs.color == rhs.color &&
            lhs.number == rhs.number &&
            lhs.shading == rhs.shading &&
            lhs.symbol == rhs.symbol) ||
            (lhs.color == rhs.color &&
            lhs.number != rhs.number &&
            lhs.shading != rhs.shading &&
            lhs.symbol != rhs.symbol) ||
            (lhs.color != rhs.color &&
            lhs.number == rhs.number &&
            lhs.shading != rhs.shading &&
            lhs.symbol != rhs.symbol) ||
            (lhs.color != rhs.color &&
            lhs.number != rhs.number &&
            lhs.shading == rhs.shading &&
            lhs.symbol != rhs.symbol) ||
            (lhs.color != rhs.color &&
            lhs.number != rhs.number &&
            lhs.shading != rhs.shading &&
            lhs.symbol == rhs.symbol) ||
            (lhs.color != rhs.color &&
            lhs.number != rhs.number &&
            lhs.shading != rhs.shading &&
            lhs.symbol != rhs.symbol)
        }

    Пытаюсь реализовать правила игры в Set протоколом Equatable. Говнокод?

    govnokoder3948, 25 Мая 2018

    Комментарии (11)
  10. Swift / Говнокод #24071

    0

    1. 1
    2. 2
    3. 3
    4. 4
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            let count = self.gameResults == nil ? 0 : self.gameResults?.count
            return count!
        }

    nil-coalescing operator? Не не слашали.

    astmus, 04 Апреля 2018

    Комментарии (19)