Does Cocoa have a LINQ equivalent?

Discussion in 'Mac Programming' started by pctechtv, Jul 23, 2017.

  1. pctechtv macrumors member

    Sep 3, 2013
    Microsoft LINQ is a component of programming in their systems. I am interested in how it can turn local assets like text and XML files into query-able content. Does Cocoa have anything like this. Thanks
  2. Krevnik, Jul 24, 2017
    Last edited: Jul 24, 2017

    Krevnik macrumors 68040


    Sep 8, 2003
    The approach to how you do something like LINQ differs quite a bit with Cocoa and Foundation.

    NSPredicate enables a similar sort of filtering on any collection of objects using KVO, although I think LINQ’s syntax is better. That gets you the querying part, and the upside is that it doesn’t really matter where the data came from. Just make sure your objects are KVO-compliant, and use the standard collection types.

    For the loading part, you need some sort of serialization. Apple provides serialization for plists and JSON, but in general has very little to no XML support baked into Foundation. You’d be looking for a third party serialization framework if you want XML in this case. Or just use JSON.

    EDIT: And if we are talking about Swift, then you have similar tools baked into the runtime for standard collections that more closely aligns with LINQ’s syntax for in-memory objects. You still need to use some form of serialization like above.

    Really, LINQ’s strength is that it can also query against something like SQL with the same syntax, allowing you to query data that isn’t in memory. While with NSPredicate, it only works that way with CoreData. I suppose with a proper SQL back end on CoreData, you could do it though, but you are looking at a 3rd party bit of code for that. The real thing that bothers me about LINQ is that they introduced yet another way to parse XML to enable it rather than extending one of the two existing ways .NET already had by making their collections complaint. Forcing you into either the new or old way of dealing with XML rather than making it easy to do both.
  3. pctechtv thread starter macrumors member

    Sep 3, 2013
    This helps point me in the correct direction. Thanks a lot!

Share This Page