MJExtension Deployment & Usage Guide
1. Prerequisites
- Xcode: Version 9.0 or later (for Swift 4+ compatibility)
- iOS/macOS Deployment Target: iOS 8.0+ or macOS 10.10+ (minimum supported by MJExtension)
- Dependency Manager (optional, if using automated installation):
- CocoaPods 1.0+
- Carthage 0.30+
- Swift Package Manager (Xcode 11+)
- Swift Projects: Swift 4.0+ (due to
@objcanddynamicrequirements)
2. Installation
Choose one method:
CocoaPods
- Add to your
Podfile:pod 'MJExtension' - Install:
pod install
Carthage
- Add to your
Cartfile:github "CoderMJLee/MJExtension" - Build:
carthage update --platform iOS - Drag
MJExtension.frameworkfromCarthage/Build/iOSto your Xcode project's "Frameworks, Libraries, and Embedded Content" section.
Swift Package Manager (Xcode 11+)
- In Xcode:
File→Swift Packages→Add Package Dependency. - Enter repository URL:
https://github.com/CoderMJLee/MJExtension - Select version (e.g.,
3.4.0or later) and add the package.
Manual Integration
- Download or clone the repository.
- Drag the entire
MJExtensionfolder from the project root into your Xcode project (ensure "Copy items if needed" is checked). - Import the main header in any file using MJExtension:
#import "MJExtension.h"
3. Configuration
Objective-C Models
No special configuration required for basic JSON ↔ model conversion. For custom behavior:
-
Custom JSON Key Mapping: Override
+mj_replacedKeyFromPropertyNamein your model:@implementation User + (NSDictionary *)mj_replacedKeyFromPropertyName { return @{@"userID" : @"id"}; } @end -
Date Formatting: Set a global date formatter block (e.g., in
AppDelegate):[NSDateFormatter mj_setupDateFormatBlock:^NSString *(NSDateFormatter *formatter) { return @"yyyy-MM-dd HH:mm:ss"; }];
Swift Models
Swift models require Objective-C runtime exposure:
- Annotate the class with
@objcor@objcMembers:@objcMembers class User: NSObject { // ... } - For non-optional primitive types (
Bool,Int,Double, etc.), mark properties asdynamicand assign default values:dynamic var age: Int = 0 dynamic var isVIP: Bool = false - Optional types (
String?,Int?, etc.) do not requiredynamic.
Core Data Integration
If using Core Data, register your entity:
[User mj_setupCoreDataEntity:^NSString *{
return @"User";
}];
4. Build & Run
Building the Library (Only for Manual Integration or Development)
- Manual Integration: The library files are compiled directly into your app target—no separate build step needed.
- Developing MJExtension Itself: Open
MJExtension.xcodeprojand build theMJExtensionscheme.
Using MJExtension in Your Project
- Create a model class (Objective-C or Swift as configured above).
- Convert JSON to model:
// Objective-C NSDictionary *json = @{@"name": @"Jack", @"age": @20}; User *user = [User mj_objectWithKeyValues:json];// Swift let json: [String: Any] = ["name": "Jack", "age": 20] let user = User.mj_object(withKeyValues: json) - Convert model to JSON:
NSDictionary *json = [user mj_keyValues]; - For arrays:
NSArray *userArray = [User mj_objectArrayWithKeyValuesArray:jsonArray];
5. Deployment
MJExtension is a static library/framework—it does not run standalone. To deploy an app using MJExtension:
- Integrate MJExtension via one of the installation methods above.
- Build your app for release (Product → Archive in Xcode).
- Distribute via:
- App Store Connect (iOS/macOS App Store)
- Enterprise ad-hoc distribution
- TestFlight (iOS)
- Direct .ipa/.app distribution (enterprise or development)
No additional runtime dependencies are required—MJExtension is compiled into your app binary.
6. Troubleshooting
Swift Properties Not Converting
- Symptom: Swift model properties remain
nilor default after JSON conversion. - Solution:
- Ensure the class inherits from
NSObject. - Add
@objcor@objcMembersto the class. - Mark primitive types (
Bool,Int, etc.) asdynamicwith default values. - Verify the JSON keys match property names (or use
mj_replacedKeyFromPropertyName).
- Ensure the class inherits from
Date Conversion Fails
- Symptom:
NSDateproperties arenilor incorrect. - Solution:
- Set a global date formatter block (see Configuration).
- For custom formats per property, implement
+mj_objectClassInArrayor useNSDateFormatterdirectly in a custom transformer.
Nested Models Not Populated
- Symptom: Model properties that are other models remain
nil. - Solution:
- Ensure nested model classes are imported (
#import "NestedModel.h"). - Verify JSON structure matches property hierarchy exactly.
- For arrays of nested models, ensure the array property is typed as
NSArray<NestedModel *> *or use+mj_objectClassInArrayto specify the class.
- Ensure nested model classes are imported (
JSON Key Mapping Not Applied
- Symptom: Custom key mapping (e.g.,
@"ID" : @"id") has no effect. - Solution:
- Confirm
+mj_replacedKeyFromPropertyNameis implemented in the model implementation file (.m), not the header. - Ensure the method returns a dictionary with JSON key as key and property name as value.
- Confirm
"Unrecognized Selector" Crashes
- Symptom: App crashes with
NSInvalidArgumentExceptionwhen callingmj_objectWithKeyValues. - Solution:
- Verify the model class is linked (for manual integration, check the
.mfile is in the target's Compile Sources). - For Swift, ensure the class is marked
@objcand inherits fromNSObject.
- Verify the model class is linked (for manual integration, check the
Carthage Framework Not Found
- Symptom: "Library not found for -lMJExtension" during build.
- Solution:
- Run
carthage update --platform iOSagain. - Ensure the framework is added to "Linked Frameworks and Libraries" and "Embedded Binaries".
- Check that the framework search
- Run