Questions tagged [swift]

Swift is a general-purpose, multi-paradigm, compiled programming language developed by Apple Inc. for iOS, iPadOS, macOS, watchOS, tvOS, Linux, and z/OS.

Filter by
Sorted by
Tagged with
4
votes
2answers
3k views

Swift 3 Generic fetch request extension

I implemented an extension for the NSManagedObjectContext that takes any CoreData class as a parameter and performs a fetch request on it. I am new to CoreData ...
4
votes
1answer
11k views

Swift FloatingPoint rounded to places

I'm playing a bit with extensions for default protocols in Swift 3.0. I would like to implement rounded(toPlaces places: Int) -> Self for ...
4
votes
1answer
106 views

Safe init using failable guarded initializer

I am wondering if that is a safe and proper init, with one failable init guarded and an init. Are the force-unwrap correct in the self.init as I know they are ...
4
votes
2answers
5k views

Recursive function to show directions for multiple locations

I have a Mapview which shows a bunch of locations (Events). Here's the code of an Event object. ...
4
votes
1answer
947 views

Second attempt at Hangman in Swift

I recently posted my Hangman game, and got some amazing feedback! The main thing suggested was to create a word generator that could read JSON files so that my hangman brain class didn't have to ...
4
votes
2answers
185 views

Fetching the definition of a word using a REST API

This is the code I'm using to GET the definition of a word from Wordnik's REST API. I don't want to ignore any error but this is causing the code to be ridiculously long. I'm new to Swift so I'd ...
4
votes
1answer
823 views

Optimization of multiple arrays for filtering

My app will be running on an iPad with A7. I pass in 3 JSON arrays and right now do some simple filtering, in the future the operation will be much more complicated. I realize the ...
4
votes
3answers
5k views

Project Euler #8 - Largest product in a series

I just finished Project Euler #8 in Swift, and since there is not any version yet on Code Review, I would like to have some comments on what I did to try to improve it. The four adjacent digits in ...
4
votes
1answer
310 views

Project Euler #6 (Sum square difference) in Swift

...
4
votes
1answer
97 views

Find minimum count of items where sum of them is X from an array

I recently faced an interview question where you have to find minimum needed items from an array that can added together to generate X value. For example giving: <...
4
votes
1answer
130 views

Parse data into an array of structs

I'd had a hard time getting this to work. I’d like to parse a file containing data and copy this data into a struct. The data file (test.dat) looks like this: <...
4
votes
1answer
258 views

Leetcode 102: Binary tree level-order traversal in Swift

102. Binary Tree Level Order Traversal Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level). For example: Given binary ...
4
votes
1answer
2k views

Iterating 2D array in Swift

I have a situation where I needed to convert NSNumber 2D array to Float 2D array, so I did it like this: ...
4
votes
2answers
773 views

Big Nerd Ranch Bronze Challenge: Disallow Alphabetic Characters

I just completed BNR's bronze challenge for disallowing alphabetic characters, and would love some feedback on what I did right and wrong, and how I can improve my code for reusability and ...
4
votes
1answer
1k views

Updating number of votes with a “Like” button

I have a "Like" button in my iOS app that updates the votes every time it's tapped. Just like Facebook, Instagram, ProductHunt, etc. I have a custom cell where that ...
4
votes
1answer
297 views

Counting the total value of Card instances given

I am trying to write a function called countHand() that takes in an array of Card instances and counts the total value of the ...
4
votes
1answer
97 views

Rock Paper Scissors in Swift

I'm going through the eBook Intro to App Development with Swift by Apple and I am making the Rock, Paper, Scissors app in lesson 20. This is the assignment that I got: [...] Name the enum ...
4
votes
1answer
82 views

Swift Prim's algorithm

Lots of Prim's algorithm implementations seem long, and rely on a priority queue implementation. If I use an adjacency matrix I can then calculate the next item to take using functional programming ...
4
votes
1answer
2k views

A consumable byte array, in Swift

For decoding binary data (in my case, delivered by a Bluetooth device), I've written this struct: ...
4
votes
2answers
187 views

Initializing an Array of Alternating Values

My goal in this project is to make something that can—as, quickly, effectively, and efficiently as Array(repeating: , count: )—initialize an ...
4
votes
1answer
239 views

iOS Swift 3 app that requests data from the Stack Exchange API

This is my first Swift app, so I want to improve everything that I can about my code and know what I can do better. The app requests 10 developers from StackExchange API and displays them in a table ...
4
votes
1answer
495 views

Writing a chunk of a file to a socket in Swift

I'm writing a chunk of a file from an offset to a socket. This is my implementation and it's working OK from my tests. Any comments or speed improvements? ...
4
votes
1answer
4k views

Finding consecutive days in an array of dates

The problem: How to get the largest number of consecutive days from an array of unknown number of Date objects, by taking into account days only like 4th of April, 5th of April and so on. Example ...
4
votes
2answers
13k views

Basic calculator in Swift 3

I just started learning the basics to Swift 3 about 2 days ago. Since then, I've decided to make a simple calculator that can only do one operation at a time. I was wondering how efficient this was ...
4
votes
1answer
1k views

Swift Hackerrank 2D Array

I solved this question in Swift, but was wondering if there is a better way or if my code can be improved: Given a 6x6 2D Array: ...
4
votes
1answer
10k views

Show and hide views

I have some buttons in my class that show or hide two views (by setting their alpha property to either 0 or 1) when the user taps on them. I have written a function that uses the ...
4
votes
2answers
2k views

Swift solution to Leetcode “Longest Substring Without Repeating Characters”

From LeetCode medium 3. Longest Substring Without Repeating Characters: Given a string, find the length of the longest substring without repeating characters. Examples: Given "abcabcbb", ...
4
votes
1answer
3k views

Greyscale converter performance in Swift

I have a single class which converts an image's colors into greyscale (except one given color, which will be left as it was before): ...
4
votes
1answer
330 views

ReactiveCocoa implementation of async function

I would like a review of the RAC implementation for the function setGist() I've posted the code on GitHub for easier reading. Description: You create an instance ...
4
votes
1answer
339 views

Using Grand Central Dispatch to fetch data from Parse.com and update UI asynchronously

This works, and the UI is snappy in the simulator, but since this is my first time really using GCD, I'd just like this code to be reviewed before I start using it everywhere. Note that this is ...
4
votes
1answer
21k views

Swift extension: Dictionary<String, AnyObject> to JSON string for URL parameter

Here's a quick extension for [String: AnyObject] dictionaries in Swift. The output needs to be in the following format (order and spacing don't matter): ...
4
votes
1answer
723 views

Save File Manager

I made a class to manage saved file in drive with Swift, and I tried to do the best work. Can someone get me a feedback about this class? What can I improve? Is this a good way to save a file? ...
4
votes
1answer
424 views

Project Euler #16 in Swift - Power digit sum

I just finished Project Euler #16 in Swift, and since there is not any version yet on Code Review, I would like to have some comments on what I did to try to improve it. 2^15 = 32768 and the sum of ...
4
votes
1answer
97 views

POC for GithubUserFinder

I would like to request iOS developer community to please review my POC i.e. [GithubUserFinder][1] which I have created as part of job screening test at T-mobile Bengaluru. I have around 2 years of ...
4
votes
1answer
84 views

Force-unwrapped property as a contract

I have a class, which is meant to show a full screen modal controller with a set of actions (apart from actions it also has some static content, a lot of labels and images which are irrelevant, so I ...
4
votes
1answer
177 views

Convert String to Integer (calculator app)

I'm currently learning Swift and today I created my first application, a simple calculator. My main goal was to practice and create something without any help. After a few hours my application was ...
4
votes
2answers
17k views

Showing activity indicator (loading image) while processing in background

I have a login screen on my iOS app. When I click login button I want to make sure that user should be displayed a loading image in center of the screen. I found lots of different methods online but ...
4
votes
1answer
154 views

Search a string for barcodes but ignore certain characters

I had to write a string search function that would search for barcodes, and be forgiving of minor formatting differences. For example, the barcode may be entered as ...
4
votes
1answer
600 views

Jumping on the clouds

I have started the Hackerrank interview preparation kit. The first problem I have solved with Swift is as follows: Emma is playing a new mobile game that starts with consecutively numbered clouds. ...
4
votes
1answer
523 views

A helper class for creating icons from a PNG image and colour in Swift

I have made a helper struct in Swift that creates circular icons from a PNG image (a white icon on transparent background) and a background ...
4
votes
1answer
765 views

Drawing a word, with a box around one letter

I'm working on a draw function which needs to draw the word 'crux' in the center of the current view and then draw a red box around the 'x' character like this: My ...
4
votes
1answer
809 views

Maintainable networking layer architecture

There are so many different solutions and examples on how to build a proper networking layer, but every app has different constraints, and design decisions are made based off trade-offs, leaving me ...
4
votes
1answer
9k views

Creating a array of custom objects from JSON, sent using PHP from a MySQL database

I'm using PHP to send data in JSON to create an array of custom objects in Swift. I've been told retrieveData() is bad code because it's in an infinite loop. I'm ...
4
votes
1answer
121 views

Swift menu code

I want to know what do you think about the structure of this code. If you think that there is some structural improvement to be made, tell me! Here is a github link: https://github.com/LucianoPolit/...
4
votes
1answer
592 views

Show a quiz from JSON in iOS

I have to create a quiz of showing questions from a JSON and post back the answers. Here's the structure of JSON: ...
4
votes
1answer
202 views

Sorting JSON items from a site

So I recently applied to this job for iOS Developer and was invited to complete the following code challenge: As a next step, please complete this coding exercise in order to proceed with our ...
4
votes
1answer
65 views

Basic network layer for a JSON API client

Just looking for some feedback/ways to improve this basic networking layer written in Swift. I'm still learning, please elaborate with as much detail as possible. ...
4
votes
1answer
299 views

Notify user of credential failure and make another attempt

The below code works because of the do operator on line 24. Is there a more functional way to get the test to pass, or would you consider this an appropriate use of ...
4
votes
1answer
68 views

iOS game of drawing sticks with AI and scoring

My code actually works. I was just wondering if there was alternative ways, maybe even more efficient ways of coding my program. This is suppose to be a game of drawing stick, and it's recording the ...
4
votes
1answer
172 views

Switch expression function

I made a bunch of functions and an enumeration, that together allow me to write something like a switch expression. It's not as powerful as Swift switch statements, since it's lacking pattern matching ...

1 2 3
4
5
13