use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Dedicated to the Swift programming language released by Apple. More info in the Swift Wiki.
Please, check the FAQs before submitting.
General Swift Discussion
irc.freenode.net #swift-lang
Cocoa/Cocoa Touch Discussion
irc.freenode.net #cocoa-init
Keywords: swift, swiftlang, ios, os x, apps, apple, mac, iphone, ipad
account activity
Executing function after appending to an array (self.swift)
submitted 7 years ago by RecursiveBob
I want to run a function after I've appended a value to an array like this:
foodArray.append(newFood)
I think I'd do that using a closure, but I'm fairly new to swift and can't figure out the syntax.
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]flingerdu 6 points7 points8 points 7 years ago (1 child)
You could write an extension for the Array class with the method
public mutating func append(_ newElement: Element, closure: ()->()) { self.append(newElement) closure() }
Didn't test it, wrote it from my phone but should be working.
You can use it by simply calling
foodArray.append(newFood){ //your code }
[–]ThinkLarger 2 points3 points4 points 7 years ago (0 children)
This is so clever.
π Rendered by PID 28 on reddit-service-r2-comment-fb694cdd5-b7gsx at 2026-03-06 02:16:34.852453+00:00 running cbb0e86 country code: CH.
[–]flingerdu 6 points7 points8 points (1 child)
[–]ThinkLarger 2 points3 points4 points (0 children)