使用Gson库,如何将JSON字符串转换ArrayList为自定义类JsonLog?基本上,JsonLog是由我的Android应用程序制作的不同类型的日志实现的接口 - 短信日志,呼叫日志,数据日志 - 这ArrayList是所有这些日志的集合.我在第6行遇到错误.
public static void log(File destination, JsonLog log) {
Collection<JsonLog> logs = null;
if (destination.exists()) {
Gson gson = new Gson();
BufferedReader br = new BufferedReader(new FileReader(destination));
logs = gson.fromJson(br, ArrayList<JsonLog>.class); // line 6
// logs.add(log);
// serialize "logs" again
}
}
Run Code Online (Sandbox Code Playgroud)
似乎编译器不理解我指的是打字ArrayList.我该怎么办?
我正在使用UIManagedDocument我的项目中使用Core Data 的子类.关键是子类返回一个单例实例,以便我的屏幕可以简单地调用它,并且托管对象上下文对于所有这些实例保持相同.
在使用之前UIManagedDocument,我需要通过打开它来准备它,如果它的文件路径已经存在,或者如果它还没有创建它.我prepareWithCompletionHandler:在子类中创建了一个便捷方法,以方便两种情况.
@implementation SPRManagedDocument
// Singleton class method here. Then...
- (void)prepareWithCompletionHandler:(void (^)(BOOL))completionHandler
{
__block BOOL successful;
// _exists simply checks if the document exists at the given file path.
if (self.exists) {
[self openWithCompletionHandler:^(BOOL success) {
successful = success;
if (success) {
if (self.documentState != UIDocumentStateNormal) {
successful = NO;
}
}
completionHandler(successful);
}];
} else {
[self saveToURL:self.fileURL forSaveOperation:UIDocumentSaveForCreating completionHandler:^(BOOL success) {
successful = success;
if (success) {
if (self.documentState …Run Code Online (Sandbox Code Playgroud) 为了检测时传出呼叫接通后,我尝试创建一个PhoneStateListener和监听TelephonyManager的CALL_STATE_RINGING,CALL_STATE_OFFHOOK和CALL_STATE_IDLE,从这个问题,但它似乎没有工作,如下所述.
首先,我在清单中注册了以下权限:
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />
Run Code Online (Sandbox Code Playgroud)
然后,一个BroadcastReceiver叫OutCallLogger映入NEW_OUTGOING_CALL每当呼出由事件:
<receiver android:name=".listener.OutCallLogger">
<intent-filter>
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
</intent-filter>
</receiver>
Run Code Online (Sandbox Code Playgroud)
接下来,我执行OutCallLogger.我设置了一个布尔值,调用noCallListenerYet以避免在调用PhoneStateListener的TelephonyManager时候附加一个新的onReceive().
public class OutCallLogger extends BroadcastReceiver {
private static boolean noCallListenerYet = true;
@Override
public void onReceive(final Context context, Intent intent) {
number = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
if (noCallListenerYet) {
final TelephonyManager tm = (TelephonyManager) context.getSystemService(
Context.TELEPHONY_SERVICE);
tm.listen(new PhoneStateListener() { …Run Code Online (Sandbox Code Playgroud) 我正在尝试为typealias符合多个协议的UITableViewCell的委托属性定义一个.这就是我想要做的,Swift抱怨我的语法错误:
// The typealias definition
typealias CellDelegate = AnyObject<UIPickerViewDataSource, UIPickerViewDelegate>
// In my UITableViewCell subclass:
weak var delegate: CellDelegate?
Run Code Online (Sandbox Code Playgroud)
"不能专门化非泛型类型AnyObject"是我得到的错误.我该怎么做呢?
如果我有一个双精度数组,每个都有两个小数位,通过一个循环完全添加它们,并打印出总数,出来的是一个超过两位小数的数字.这很奇怪,因为从理论上讲,添加两个数字,每个数字只有2个,只有2个小数位,所以不会产生一个超过百分位数的非零数字的数字.
尝试执行此代码:
double[] d = new double[2000];
for (int i = 0; i < d.length; i++) {
d[i] = 9.99;
}
double total = 0,00;
for (int i = 0; i < d.length; i++) {
total += d[i];
if (("" + total).matches("[0-9]+\\.[0-9]{3,}")) { // if there are 3 or more decimal places in the total
System.out.println("total: " + total + ", " + i); // print the total and the iteration when it occured
}
}
Run Code Online (Sandbox Code Playgroud)
在我的电脑中,打印出:
total: 59.940000000000005, 5 …Run Code Online (Sandbox Code Playgroud) 我有一个对象FormField,它有两个属性:一个String name,一个value可以接受任何类型的东西 - 因此我已经成功了Any!.但是,我在一个单独的问题中被告知要使用带有相关值的枚举而不是Any!.
enum Value {
case Text(String!)
case CoreDataObject(NSManagedObject!)
}
class FormField {
var name: String
var value: Value?
// initializers...
}
Run Code Online (Sandbox Code Playgroud)
然而,这种方法使得检查无效非常冗长.如果我想显示表单中所有缺少字段的警报视图,我将不得不对switch语句中的每个案例重复nil检查:
for field in self.fields {
if let value = field.value {
switch value {
case .Text(let text):
if text == nil {
missingFields.append(field.name)
}
case .CoreDataObject(let object):
if object == nil {
missingFields.append(field.name)
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
无论类型如何,是否有更短的方式来访问枚举的相关值?如果我创建FormField.value Any!,上面的代码就像下面这样简单:
for field in self.fields {
if …Run Code Online (Sandbox Code Playgroud) 我想要一个lazily-initialized属性,如果我将属性设置为nil,我可以再次调用它.
如果我这样定义我的属性:
lazy var object = { /*init code*/ }()
Run Code Online (Sandbox Code Playgroud)
...然后调用属性,初始化程序被触发一次.但是,如果我object稍后在程序中设置为nil,则不会再次调用初始值设定项.我怎么能在Swift中做到这一点?
我查看了计算属性但它们实际上并不存储值,所以每当我调用变量时,总会发生计算或初始化.我只想在属性为零时计算.
我自己观察到的唯一区别是respondsToSelector接收器可以是类或实例,而instancesRespondToSelector只能有类接收器.还有什么能让他们与众不同?一个或另一个是否存在任何性能问题?
我的应用程序试图计算通过WiFi/LAN和移动数据连接发送和接收的字节数.为此,我TrafficStats在一个时间点获取计数器的值,并在下次检查时从其值中减去该值.
// get current values of counters
long currentMobileTxBytes = TrafficStats.getMobileTxBytes();
long currentMobileRxBytes = TrafficStats.getMobileRxBytes();
long totalTxBytes = TrafficStats.getTotalTxBytes();
long totalRxBytes = TrafficStats.getTotalRxBytes();
// to get mobile data count, subtract old from current
long currentMobileSent = currentMobileTxBytes - oldMobileTxBytes;
long currentMobileReceived = currentMobileRxBytes - oldMobileRxBytes;
// to get WiFi/LAN data count, subtract total from mobile
long currentNetworkSent = totalTxBytes - currentMobileTxBytes;
long currentNetworkReceived = totalRxBytes - currentMobileRxBytes;
Run Code Online (Sandbox Code Playgroud)
我觉得上面的算法是合理的,但是,我不知道如何检查这些计数器的准确性.例如,当我尝试通过WiFi将2.7MB文件上传到Dropbox时,currentMobileSent我得到的值大约是10MB.即使没有浏览网页直到下一次检查,我得到非零值,表明我在等待期间确实收到了一些字节的数据.
有没有办法让我检查TrafficStats这些数字是如何到达的?我知道除了我的浏览器,可能还有其他应用程序在后台运行连接到互联网,但2.7MB到10MB似乎是一个巨大的跳跃 - 我甚至"没有做任何事情"一次"收到"90MB.或者我计算发送和接收的字节的方式有问题吗?
我这样做的原因是,作为日期对象存储在任何数据库中的日期往往以特定格式编写,这可能与您需要在前端向用户呈现的内容大不相同.我还认为,如果您的应用程序从不同类型的数据存储中提取信息,这将特别有用.一个很好的例子是MongoDB和SQL日期对象之间的区别.
但是,我不知道这是否是推荐的做法.我应该将日期保存为长(以毫秒为单位)或作为日期对象吗?