Objective-C, Quick Tips

Constructing an NSArray with NSString Copies

The scenario might sound specific, but I am confident you’ve encountered something similar before. You need to construct an array, with a known number of duplicates of a string. Perhaps you’re constructing a template, and need a fixed number of placeholder elements, or you’re parameterizing a query and need a dynamic number of placeholders. In either case, you were probably left writing a rather ugly bit of logic in the middle of a routine that was otherwise focused on the task at hand.

I found myself in a similar situation and put together the following Objective-C category:


There is not much going on here, but the nice thing is that it provides a clean interface for constructing an NSArray with a specified number of duplicates.

Hopefully this saves you time on your next iOS or Mac app!

About Jason

Jason is an experienced Entrepreneur & Software Developer with a demonstrated history of working in the oil & energy industry. Skilled in Leadership, Mobile Development, Data Synchronization, and SaaS Architecture. Strong engineering professional with a Bachelor of Science (B.S.) in Computer Science from Arkansas State University.
View all posts by Jason →

Leave a Reply

Your email address will not be published. Required fields are marked *