AAChartModel / AAChartKit

📈📊🚀🚀🚀An elegant modern declarative data visualization chart framework for iOS, iPadOS and macOS. Extremely powerful, supports line, spline, area, areaspline, column, bar, pie, scatter, angular gauges, arearange, areasplinerange, columnrange, bubble, box plot, error bars, funnel, waterfall and polar chart types. 极其精美而又强大的现代化声明式数据可视化图表框架,支持柱状图、条形图、折线图、曲线图、折线填充图、曲线填充图、气泡图、扇形图、环形图、散点图、雷达图、混合图等各种类型的多达几十种的信息图图表,完全满足工作所需.
https://cocoapods.org/pods/AAChartKit
MIT License
4.71k stars 750 forks source link

dictionaryWithObjectInstance 这个模型转字典,能用MJExtension替换掉吗? #1417

Closed kedu closed 1 year ago

kedu commented 1 year ago

dictionaryWithObjectInstance 这个模型转字典,能用MJExtension替换掉吗?主要是开启Optimization Level为faster的时候会在dictionaryWithObjectInstance方法里崩溃

AAChartModel commented 1 year ago

你可以试着为 AAJsonConverter 添加分类方法, 来覆盖原有的 dictionaryWithObjectInstance 方法.

AAChartModel commented 1 year ago

示例如下:

创建 AAJsonConverter+MJExtension 分类文件,

.h 文件内容为:

//
//  AAJsonConverter+MJExtension.h
//  AAChartKit-ProDemo
//
//  Created by AnAn on 2022/11/1.
//  Copyright © 2022 An An. All rights reserved.
//

#import "AAChartView.h"

NS_ASSUME_NONNULL_BEGIN

@interface AAJsonConverter (MJExtension)

/// Convert Object to be Dictionary
/// @param objc  object instance
+ (NSDictionary*)dictionaryWithObjectInstance:(id)objc;

@end

NS_ASSUME_NONNULL_END

.m 文件内容为:

//
//  AAJsonConverter+MJExtension.m
//  AAChartKit-ProDemo
//
//  Created by AnAn on 2022/11/1.
//  Copyright © 2022 An An. All rights reserved.
//

#import "AAJsonConverter+MJExtension.h"

@implementation AAJsonConverter (MJExtension)

/// Convert Object to be Dictionary
/// @param objc  object instance
+ (NSDictionary*)dictionaryWithObjectInstance:(id)objc {
    NSLog(@"------------------🔥🔥🔥🔥🔥🔥🔥🔥");
    NSLog(@"为 AAJsonConverter 添加分类方法, 查看 AAJsonConverter 原有 dictionaryWithObjectInstance 方法是否被覆盖了");
    NSLog(@"------------------🔥🔥🔥🔥🔥🔥🔥🔥");
    return NSDictionary.dictionary;
}

@end

添加完成后, 运行 demo, 点击 demo 中的某一个图表示例, 会发现控制台有以下输出:

2022-11-01 11:11:23.090018+0800 AAChartKit-ProDemo[2297:41149] ------------------🔥🔥🔥🔥🔥🔥🔥🔥
2022-11-01 11:11:23.090134+0800 AAChartKit-ProDemo[2297:41149] 为 AAJsonConverter 添加分类方法, 查看 AAJsonConverter 原有 dictionaryWithObjectInstance 方法是否被覆盖了
2022-11-01 11:11:23.090217+0800 AAChartKit-ProDemo[2297:41149] ------------------🔥🔥🔥🔥🔥🔥🔥🔥

说明已成功完成 AAJsonConverter 的 dictionaryWithObjectInstance 方法的覆写.

AAChartModel commented 1 year ago

不过直接这样写, Xcode 会有警告⚠️, 如图所示:

截屏2022-11-01 11 18 03
AAChartModel commented 1 year ago

想要忽略警告⚠️, 参考:

为 Xcode 编译器添加忽略警告⚠️的内容:

示例如下:

//
//  AAJsonConverter+MJExtension.m
//  AAChartKit-ProDemo
//
//  Created by AnAn on 2022/11/1.
//  Copyright © 2022 An An. All rights reserved.
//

#import "AAJsonConverter+MJExtension.h"

@implementation AAJsonConverter (MJExtension)

//https://stackoverflow.com/questions/9424004/suppress-warning-category-is-implementing-a-method-which-will-also-be-implement
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wobjc-protocol-method-implementation"
/// Convert Object to be Dictionary
/// @param objc  object instance
+ (NSDictionary*)dictionaryWithObjectInstance:(id)objc {
    NSLog(@"------------------🔥🔥🔥🔥🔥🔥🔥🔥");
    NSLog(@"为 AAJsonConverter 添加分类方法, 查看 AAJsonConverter 原有 dictionaryWithObjectInstance 方法是否被覆盖了");
    NSLog(@"------------------🔥🔥🔥🔥🔥🔥🔥🔥");
    return NSDictionary.dictionary;
}

#pragma clang diagnostic pop

@end
AAChartModel commented 1 year ago

由于希望减少 Cocoapods 管理复杂度, 和避免增加不必要的代码量和包体积. AAChartKit 本身不想再额外添加第三方依赖.

所以采用此方法来解决你需要的使用 MJExtension 来替换 AAJsonConverter 的 dictionaryWithObjectInstance .

kedu commented 1 year ago

不过也有问题,就是一个模型接受数组的话,用mj开启Optimization Level为faster也是有问题的。能不能换种方式,数组就用数组来接,单个就用模型来接。

AAChartModel commented 1 year ago

就是一个模型接受数组的话

不太清楚你这里的具体指的是什么?

AAChartModel commented 1 year ago

能不能换种方式,数组就用数组来接,单个就用模型来接

AAJsonConverter 添加分类后, 你可以重写原有方法了, 所以想要添加什么内容, 你完全可以自行实现.

kedu commented 1 year ago

我说的是这个

aaOptions.xAxisSet(aaXAxis)
    .touchEventEnabledSet(YES)
    .titleSet(aaTitle)
    .yAxisSet((id)@[aaYAxis1,aaYAxis2])

这里yAxisSet一会接收单个模型,一会接收数组

AAChartModel commented 1 year ago

不过也有问题,就是一个模型接受数组的话,用mj开启Optimization Level为faster也是有问题的。能不能换种方式,数组就用数组来接,单个就用模型来接。

你能在 AAChartKit 的 demo 中复现问题吗? 为什么我在 demo 中设置了之后, 真机运行 demo, 查看双 Y轴和多 Y轴的图表示例 , 一切正常?

截屏2022-11-01 14 13 38
kedu commented 1 year ago

选-03看看

AAChartModel commented 1 year ago

选-03看看

试了, 一样是正常的

kedu commented 1 year ago

好的,我再排查一下