Skip to content

Instantly share code, notes, and snippets.

View phynet's full-sized avatar
🤘
I said yeah.

Sofia Swidarowicz phynet

🤘
I said yeah.
View GitHub Profile
@phynet
phynet / observer_updateData.m
Created September 14, 2015 06:38
Using NSNotification to update data
__typeof__(self) __weak wself = self;
[[NSNotificationCenter defaultCenter] addObserverForName:UIApplicationWillEnterForegroundNotification object:nil queue:nil usingBlock:^(NSNotification *note) {
[wself updateData];
}];

Previous Swift 2.0, the option .character for String's type, was obtained using advance and startIndex (pretty much a pain in the butt, so make it an extension and you could have the characters :P )

var kString = "My string"
var countString = count(kString)

for i in 0...countString - 1 {
  let index = advance(kString.startIndex, i)
  print("INSIDE FOR \(kString[index])")
  self.labelAmount.text?.append(kString[index])

Taken from Stackoverflow

func f(n: Int) {
  func lap(n: Int) -> Int {
    if n == 0 { return 0 }
    print(n)
    return lap(n - 1)
  }
  lap(n)

}

Resize Keyboard Swift

We start by adding observers with notificationCenter, for UIKeyboardWillShowNotification and UIKeyboardWillHideNotification actions.

Next, we start setting duration and curve animation for keyabord. Updating values for autolayout constraints (drag and drop constraint from Storyboard in your swift class), and performing a custom animation that will show our keyboard.

I've also added a method for touch, that will hide keyboard if touches are not in UITextView or UITextField

    override func viewDidLoad() {
       keyboardResizeObserver()

##Translate removeObjectAtIndex from NSMutableArray into Array in Swift

If you were wondering how in hearth you could removeObject from an Array declared in Swift... I was in your same predicament. Thankfully there are people out there that came first, and found a solution. I'm goin to translate it here for you all and of course, for me...

###Objective-C code:

[self.selectedRows removeObject:[NSNumber numberWithLong:indexPath.row]];
@phynet
phynet / Setting shadow's button in Swift 1.2.md
Last active January 20, 2017 08:04
Setting shadow's button in Swift 1.2

##Setting shadow's button in Swift 1.2

  func setShadowButton(button: UIButton){
    button.layer.shadowColor = UIColor.blackColor().CGColor
    button.layer.shadowOffset = CGSizeMake(0.0, 2.0)
    button.layer.masksToBounds = false

button.layer.shadowRadius = 1.0

@phynet
phynet / Setting-spaces-between-characters-Swift.md
Last active March 10, 2022 09:15
Setting spaces between characters with Swift

##Setting spaces between characters with Swift (iOS)

###UIButton

    @IBOutlet weak var button: UIButton!

    button.titleLabel?.attributedText = NSAttributedString(string: string, attributes:[ NSKernAttributeName: 1.3])
@phynet
phynet / Using UIAlertController with NSLocalizedString.md
Last active August 2, 2016 08:02
Using UIAlertController with NSLocalizedString

##Using UIAlertController with NSLocalizedString Because I tend to localize everything in my apps, UIAlertController needed to be localized as well. So, I wrote a struct to simplify the way I use NSLocalizedWithString (which happens to be a long piece of code), and so I could use it in all my classes.

Use an extension (remember, an extension is like a category in Obj-c)

extension String {

   var localized: String {
      return String.localizedStringWithFormat(NSLocalizedString(self,comment: ""))

}

Using NSJSONSerialization with Swift <= 1.2

As a note, I will recomend you to use below code when using NSError

var err: AutoreleasingUnsafeMutablePointer<NSError?> = nil

##Function

func getJSONValues(data: NSData) -> NSDictionary?{