Skip to content

Instantly share code, notes, and snippets.

View novinfard's full-sized avatar

Soheil Novinfard novinfard

View GitHub Profile
@novinfard
novinfard / MyCoredataObject+CoreDataProperties.swift
Last active December 5, 2022 23:10
[Auto increment in Core Data] How to implement auto-increment in core data while saving it #coredata
import Foundation
import CoreData
extension MyCoredataObject {
@nonobjc public class func createFetchRequest() -> NSFetchRequest<MyCoredataObject> {
return NSFetchRequest<MyCoredataObject>(entityName: "MyCoredataObject")
}
@NSManaged public var sortId: Int64
@novinfard
novinfard / playWithStrings.swift
Created June 18, 2018 15:56
[Working with Strings]
import Foundation
var stringOne = "Hello llollo"
var stringTwo = ""
stringOne.isEmpty
stringTwo.isEmpty
stringOne == "Hello llollo"
stringOne == "hello"
@novinfard
novinfard / enumSimple.swift
Created June 18, 2018 20:12
[Simple enumerations in Swift]
import Foundation
enum TestPhases {
case Beta
case Alpha
case Stable
}
enum TestPhasesNew: String {
case Beta = "Beta :)"
@novinfard
novinfard / enumWithAssociative.swift
Created June 18, 2018 20:21
[Enumeration with Associative members]
import Foundation
enum Product {
case Book(Int, String)
case Car(Int, Int, String)
}
let AhmadShahBook = Product.Book(10, "Ahmad Shah")
switch AhmadShahBook {
@novinfard
novinfard / Alamofire.request.error.handling.swift
Created June 22, 2018 19:57 — forked from perlguy99/Alamofire.request.error.handling.swift
Alamofire Request Error Handling - From their documentation
Alamofire.request(urlString).responseJSON { response in
guard case let .failure(error) = response.result else { return }
if let error = error as? AFError {
switch error {
case .invalidURL(let url):
print("Invalid URL: \(url) - \(error.localizedDescription)")
case .parameterEncodingFailed(let reason):
print("Parameter encoding failed: \(error.localizedDescription)")
print("Failure Reason: \(reason)")
@novinfard
novinfard / gcd-multiple-tasks.swift
Created June 23, 2018 17:41
[Working with multiple tasks in GCD]
let myGroup = DispatchGroup()
var result = [Data]()
for i in 0 ..< 5 {
myGroup.enter()
Alamofire.request("https://httpbin.org/get", parameters: ["foo": "bar"]).responseJSON { response in
print("Finished request \(i)")
result.append(response.data)
myGroup.leave()
@novinfard
novinfard / closure.swift
Last active June 24, 2018 11:25
[Closures in Swift]
//** simple form
// simple
let clos1 = { () -> Void in
print("Hello World!")
}
clos1()
// simple 2
let clos2 = { (name: String) -> Void in
print("Hello \(name)!")
@novinfard
novinfard / closure-usage.swift
Last active June 25, 2018 00:37
[Usage of Closures]
let guests = ["Ahmad", "Mohsen", "Ehsan"]
// map closure
guests.map {
print("Hello \($0)")
}
let messages = guests.map { (name) -> String in
return name + "!"
}
print(messages)
@novinfard
novinfard / documentation.swift
Last active August 30, 2018 12:00
[Swift Documentation comments]
/// Text like this appears in "Description".
///
/// Leave a blank line to separate further text into paragraphs.
///
/// You can use bulleted lists (use `-`, `+` or `*`):
///
/// - Text can be _emphasised_
/// - Or **strong**
///
/// Or numbered lists:
@novinfard
novinfard / getCarrier.swift
Created September 10, 2018 10:26
[get Mobile Carrier name in iOS]
+ (NSString *) getCarrierName
{
CTTelephonyNetworkInfo *netinfo = [[CTTelephonyNetworkInfo alloc] init];
CTCarrier *carrier = [netinfo subscriberCellularProvider];
NSString *carrierName = [carrier carrierName];
if (carrierName == nil) {
return @"unknown";
}