0

私は NSInvalidArgumentException を取得しています:

*** キャッチされない例外 'NSInvalidArgumentException' が原因でアプリを終了します。理由: 'サポートされていない関数型が SQL ストアに渡されました'

「multiply:by:」関数を使用しているネストされた NSExpression の平方根を取得するために NSExpression を使用しようとする場合。

私のコードは次のようになります。

NSManagedObjectContext *context=[[BDCoreDataController sharedController] mainManagedObjectContext];
NSFetchRequest *theRequest=[[NSFetchRequest alloc] init];

[theRequest setEntity:[NSEntityDescription entityForName:@"BDCompanyEntity" inManagedObjectContext:context]];
theRequest.resultType=NSDictionaryResultType;

NSExpression *constantExpression=[NSExpression expressionForConstantValue:[NSNumber numberWithDouble:22.5]];
NSExpression *firstKeyPath=[NSExpression expressionForKeyPath:@"epsTTM"];
NSExpression *secondKeyPath=[NSExpression expressionForKeyPath:@"bookValuePerShare"];

NSExpression *firstMultiplyExpression=[NSExpression expressionForFunction:@"multiply:by:" arguments:@[firstKeyPath,secondKeyPath]];

NSExpression *secondMultiplyExpression=[NSExpression expressionForFunction:@"multiply:by:" arguments:@[firstMultiplyExpression,constantExpression]];

NSExpression *sqrtExpression=[NSExpression expressionForFunction:@"sqrt:" arguments:@[secondMultiplyExpression]];

NSExpressionDescription *expressionDescription=[[NSExpressionDescription alloc] init];
[expressionDescription setName:@"grahamNumber"];
[expressionDescription setExpression:sqrtExpression];
[expressionDescription setExpressionResultType:NSDoubleAttributeType];

[theRequest setPropertiesToFetch:@[expressionDescription,@"name"]];

NSError *fetchError;
NSArray *grahams=[context executeFetchRequest:theRequest error:&fetchError];

ネストされた "multiply:by:" を評価しようとしてフェッチを実行すると、次のコードで問題なく動作します。

NSManagedObjectContext *context=[[BDCoreDataController sharedController] mainManagedObjectContext];
NSFetchRequest *theRequest=[[NSFetchRequest alloc] init];

[theRequest setEntity:[NSEntityDescription entityForName:@"BDCompanyEntity" inManagedObjectContext:context]];
theRequest.resultType=NSDictionaryResultType;

NSExpression *constantExpression=[NSExpression expressionForConstantValue:[NSNumber numberWithDouble:22.5]];
NSExpression *firstKeyPath=[NSExpression expressionForKeyPath:@"epsTTM"];
NSExpression *secondKeyPath=[NSExpression expressionForKeyPath:@"bookValuePerShare"];

NSExpression *firstMultiplyExpression=[NSExpression expressionForFunction:@"multiply:by:" arguments:@[firstKeyPath,secondKeyPath]];

NSExpression *secondMultiplyExpression=[NSExpression expressionForFunction:@"multiply:by:" arguments:@[firstMultiplyExpression,constantExpression]];

NSExpressionDescription *expressionDescription=[[NSExpressionDescription alloc] init];
[expressionDescription setName:@"grahamNumber"];
[expressionDescription setExpression: secondMultiplyExpression];
[expressionDescription setExpressionResultType:NSDoubleAttributeType];

[theRequest setPropertiesToFetch:@[expressionDescription,@"name"]];

NSError *fetchError;
NSArray *grahams=[context executeFetchRequest:theRequest error:&fetchError];

最初は、おそらく "sqrt" NSExpression がネストされた NSExpression で問題を抱えているのではないかと考えました。だから私は定数 NSExpression だけを試しました。次のコードは、sqrt NSExpression で定数を評価しようとします。同じ NSInvalidArguementException を引き起こします

NSManagedObjectContext *context=[[BDCoreDataController sharedController] mainManagedObjectContext];
NSFetchRequest *theRequest=[[NSFetchRequest alloc] init];

[theRequest setEntity:[NSEntityDescription entityForName:@"BDCompanyEntity" inManagedObjectContext:context]];
theRequest.resultType=NSDictionaryResultType;

NSExpression *constantExpression=[NSExpression expressionForConstantValue:[NSNumber numberWithDouble:22.5]];

NSExpression *sqrtExpression=[NSExpression expressionForFunction:@"sqrt:" arguments:@[constantExpression]];

NSExpressionDescription *expressionDescription=[[NSExpressionDescription alloc] init];
[expressionDescription setName:@"grahamNumber"];
[expressionDescription setExpression:sqrtExpression];
[expressionDescription setExpressionResultType:NSDoubleAttributeType];

[theRequest setPropertiesToFetch:@[expressionDescription,@"name"]];

NSError *fetchError;
NSArray *grahams=[context executeFetchRequest:theRequest error:&fetchError];

何が問題を引き起こしている可能性がありますか?

ありがとう

道具屋

4

2 に答える 2

3

NSExpressionSQLite ストアを使用する場合、Core Data からフェッチするときに有効なすべての式が有効であるとは限りません。残念ながらsqrt:、この種の使用はサポートされていません。おそらくバイナリ ストアで動作しますが、それでは Core Data の利点の多くが失われます (たとえば、すべてのデータを一度にメモリにロードすることを意味します)。

特に厄介なのは、どの関数が Core Data でサポートされているかを示す文書化されたリストがないことです。しかし、エラー メッセージは正確です。この関数は での使用には有効ですNSExpressionが、SQLite 永続ストアでの使用には有効ではありません。

于 2015-05-14T17:34:16.407 に答える
0

sqrt:機能は v10.5 以降の OS X では利用できるようですが、iOS では利用できません。

ドキュメントを参照してください: https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSExpression_Class/index.html#//apple_ref/occ/clm/NSExpression/expressionForFunction:arguments:

于 2015-05-14T06:32:31.540 に答える