{"id":18809978,"url":"https://github.com/aryaxt/magicalmapper","last_synced_at":"2025-04-13T20:29:40.787Z","repository":{"id":20245072,"uuid":"23517527","full_name":"aryaxt/MagicalMapper","owner":"aryaxt","description":"A Core Data managed object mapper for Swift","archived":false,"fork":false,"pushed_at":"2015-03-13T16:09:02.000Z","size":456,"stargazers_count":5,"open_issues_count":1,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2023-06-03T03:05:13.079Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":"fancyapps/fancybox","license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/aryaxt.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-08-31T16:15:08.000Z","updated_at":"2018-10-16T03:05:47.000Z","dependencies_parsed_at":"2022-09-11T01:12:42.198Z","dependency_job_id":null,"html_url":"https://github.com/aryaxt/MagicalMapper","commit_stats":null,"previous_names":[],"tags_count":null,"template":null,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aryaxt%2FMagicalMapper","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aryaxt%2FMagicalMapper/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aryaxt%2FMagicalMapper/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aryaxt%2FMagicalMapper/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/aryaxt","download_url":"https://codeload.github.com/aryaxt/MagicalMapper/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":223602932,"owners_count":17172005,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-07T23:18:23.951Z","updated_at":"2024-11-07T23:18:25.737Z","avatar_url":"https://github.com/aryaxt.png","language":"Swift","funding_links":[],"categories":[],"sub_categories":[],"readme":"MagicalMapper\n=============\n\nI started this library hoping that Swift would eventually support reflection wehich would lead to some really cool features, but that hasn't happened yet.\n\nUntil then you can use the objective c version I've written that supports both NSObjects and NSManagedObjects https://github.com/aryaxt/OCMapper\n\nMagicalMapper is a mapping library that takes a dictionary of key/values and maps them to Core Data managed objects.\n\n\nLet's take a look at an example. Below is how our core data models are defined\n\n![alt tag](https://github.com/aryaxt/MagicalMapper/blob/master/modesl.png)\n\n```swift\nclass User: NSManagedObject {\n\n    @NSManaged var id: NSNumbe\n    @NSManaged var firstName: String\n    @NSManaged var lastName: String\n    @NSManaged var age: NSNumber\n    @NSManaged var address: Address\n    @NSManaged var createdAt: NSDate\n    @NSManaged var posts: NSSet\n}\n\nclass Address: NSManagedObject {\n\n    @NSManaged var id: NSNumbe\n    @NSManaged var city: String\n    @NSManaged var country: String\n    @NSManaged var user: NSSet\n\n}\n\nclass Post : NSManagedObject {\n    \n    @NSManaged var id: NSNumber\n    @NSManaged var title: String\n    @NSManaged var body: String\n    \n}\n```\nWe make an http call that returns the following data\n```json\n{\n  \"id\"          : 1,\n  \"firstName\"   : \"Aryan\",\n  \"lastName\"    : \"Ghassemi\",\n  \"age\"         : 27,\n  \"createdAt\"   : \"2014-08-31\",\n  \"address\"     : {\n                    \"id\"      : 1,\n                    \"city\"    : \"San Francisco\",\n                    \"country\" : \"United States\"\n                  },\n  \"posts\"       : [\n                    {\n                      \"id\"    : 2,\n                      \"title\" : \"Some title\",\n                      \"body\"  : \"Some body\"\n                    },\n                    {\n                      \"id\"    : 3,\n                      \"title\" : \"Some title\",\n                      \"body\"  : \"Some body\"\n                    }\n                  ]\n  \n}\n```\nUsage\n---------\nNow let's use MagicalMapper to convert this result into our models\n```swift\nvar user = mapper.mapDictionary(userDictionary, toType: User.self)\n```\n\nIt's really that simple. MagicalMapper uses entity information to automatically generate managed objects and all associated relationships for you.\n\nWhat if the server response includes an array of users? Still as simple as getting a single object\n```swift\nvar arrayOfUsers = mapper.mapDictionaries(arrayOfUserDictionaries, toType: User.self)\n```\n\nCustom Mapping\n---------\nWhat if the key values comeing back from the server don't match our models? Well we can write custom mapping. let's say the server sends the key \"location\" instead of \"address\".\n```Swift\nmapper[User.self] = [\n    \"location\"      : \"address\",\n    \"ANOTHER_KEY\"   : \"ANOTHER_PROPERTY\"\n    ]\n```\n\nThe great thing is just because a single key in the dictionary is different from our model proprty name, it doesn't mean that we need to provide full mapping for the class. We only provide mapping where it's needed.\n\nDate Conversion\n---------\nMagicalMapper uses a list of default date formatters to automatically map NSDate properties. Here is the list of date formats in the order they are used for conversion\n```\nyyyy-MM-dd'T'HH:mm:ss.SSSSSSSZ\nyyyy-MM-dd'T'HH:mm:ss'Z'\nMM/dd/yyyy HH:mm:ss aaa\nyyyy-MM-dd HH:mm:ss\nMM/dd/yyyy\nyyyy-MM-dd\n```\n\nYou could also add your own NSDateFormatter if you need. If your application uses a consistant date format it'll perform better to add the date formatter to the mapper. Any NSDateFormatter added goes to index 0 and mapper starts with that formatter for dateconversion which helps with performance.\n```swift\nvar dateFormatter = NSDateFormatter()\ndateFormatter.dateFormat = \"MY_DATE_FORMAT\"\nmapper.addDateFormatter(dateFormatter)\n```\n\nInsert \u0026 Update\n---------\n\nWhat if we don't want to add a duplicate record everything mapping is performed? Well there is a solution for that too.\n```Swift\nmapper.addUniqueIdentifiersForEntity(User.self, identifiers: \"id\")\nmapper.addUniqueIdentifiersForEntity(Address.self, identifiers: \"id\")\nmapper.addUniqueIdentifiersForEntity(Post.self, identifiers: \"id\", \"ANOTHER_KEY\")\n```\nYou can pass an array of property names to uniquely identify each record, and MagicalMapper uses these keys to decide whether it should insert a new record or update an existing record. Supperted property types to be used as unique identifiers are Stirng, Int, NSDate.\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faryaxt%2Fmagicalmapper","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Faryaxt%2Fmagicalmapper","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faryaxt%2Fmagicalmapper/lists"}