- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在处理我的一个处于维护阶段的项目,所以我没有太多的自由来一次更改一个主要 block ,在处理这个时,我在模拟器和设备上都看到了这个随机崩溃而应用程序与服务器同步。流动性登录->上传本地数据到服务器->从服务器下载数据。
我能够调试的唯一一点是我附上的屏幕截图,我无法从中得出很多推论。
网络通信是通过自定义类促进的,如下所示
httpClient.h ------
#define TIMEOUT_SEC 30.0
@protocol HttpClientEventHandler_iPhone
@optional
- (void)requestSucceeded;
- (void)requestFailed:(NSError*)error;
@end
@class Reachability;
@interface HttpClient_iPhone : NSObject{
NSURLConnection *connection;
NSMutableData *recievedData;
int statusCode;
id delegate;
Reachability* hostReachable;
BOOL networkChecked;
}
@property (nonatomic, retain) NSMutableString *previousRequest;
@property (readonly) NSMutableData *recievedData; // XX should be (readonly, retain)
@property (readonly) int statusCode;
@property (nonatomic, assign) id delegate;
+ (NSString*)stringEncodedWithBase64:(NSString*)str;
+ (NSString*) stringOfAuthorizationHeaderWithUsername:(NSString*)username password:(NSString*)password;
- (NSMutableURLRequest*)makeRequest:(NSString*)url;
- (NSMutableURLRequest*)makeRequest:(NSString*)url username:(NSString*)username password:(NSString*)password;
- (void)prepareWithRequest:(NSMutableURLRequest*)request;
- (void)requestGET:(NSString*)url;
- (void)requestPOST:(NSString*)url body:(NSString*)body type:(NSString*)type;
- (void)requestGET:(NSString*)url username:(NSString*)username password:(NSString*)password;
- (void)requestPOST:(NSString*)url username:(NSString*)username password:(NSString*)password body:(NSString*)body type:(NSString*)type;
- (void)requestPOST:(NSString*)url username:(NSString*)username password:(NSString*)password bodydata:(NSData*)body contenttype:(NSString*)type;
- (void)uploadImage:(NSString*)requesturl
username:(NSString*)username
password:(NSString*)password
imagename:(NSString*)imagename
contenttype:(NSString*)contenttype
imagedata:(NSData*)imagedata;
- (void)cancelTransaction;
- (void)reset;
- (BOOL)checkNetworkStatus;
@end
//HttpClient.m ----------
#import "HttpClient_iPhone.h"
#import "Base64.h"
#import "Reachability.h"
#import "SyncLiteral.h"
@implementation HttpClient_iPhone
@synthesize recievedData, statusCode;
@synthesize delegate,previousRequest;
- (id)init {
if (self = [super init]) {
[self reset];
delegate = nil;
networkChecked = NO;
}
return self;
}
- (void)dealloc {
[connection release];
[recievedData release];
[super dealloc];
}
- (void)reset {
[recievedData release];
recievedData = [[NSMutableData alloc] init];
[connection release];
connection = nil;
statusCode = 0;
networkChecked = NO;
}
+ (NSString*)stringEncodedWithBase64:(NSString*)str {
static const char *tbl = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
const char *s = [str UTF8String];
int length = [str length];
char *tmp = malloc(length * 4 / 3 + 4);
int i = 0;
int n = 0;
char *p = tmp;
while (i < length) {
n = s[i++];
n *= 256;
if (i < length) n += s[i];
i++;
n *= 256;
if (i < length) n += s[i];
i++;
p[0] = tbl[((n & 0x00fc0000) >> 18)];
p[1] = tbl[((n & 0x0003f000) >> 12)];
p[2] = tbl[((n & 0x00000fc0) >> 6)];
p[3] = tbl[((n & 0x0000003f) >> 0)];
if (i > length) p[3] = '=';
if (i > length + 1) p[2] = '=';
p += 4;
}
*p = '\0';
NSString *ret = [NSString stringWithCString:(const char*)tmp encoding: NSUTF8StringEncoding];
free(tmp);
return ret;
}
#pragma mark - HTTP Request creating methods
- (NSMutableURLRequest*)makeRequest:(NSString*)url {
NSString *encodedUrl = (NSString*)CFURLCreateStringByAddingPercentEscapes(
NULL, (CFStringRef)url, NULL, NULL, kCFStringEncodingUTF8);
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:encodedUrl]];
[request setCachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData];
[request setTimeoutInterval:TIMEOUT_SEC];
[request setHTTPShouldHandleCookies:FALSE];
[encodedUrl release];
return request;
}
- (NSMutableURLRequest*)makeRequest:(NSString*)url username:(NSString*)username password:(NSString*)password {
NSMutableURLRequest *request = [self makeRequest:url];
[request setValue:[HttpClient_iPhone stringOfAuthorizationHeaderWithUsername:username password:password]
forHTTPHeaderField:@"Authorization"];
return request;
}
+ (NSString*) stringOfAuthorizationHeaderWithUsername:(NSString*)username password:(NSString*)password {
return [@"Basic " stringByAppendingString:[HttpClient_iPhone stringEncodedWithBase64:
[NSString stringWithFormat:@"%@:%@", username, password]]];
}
- (void)prepareWithRequest:(NSMutableURLRequest*)request {
// do nothing (for OAuthHttpClient)
}
#pragma mark -
#pragma mark HTTP Transaction management methods
/* Sending the Http Request for "GET" */
- (void)requestGET:(NSString*)url {
//Reseting the http client
[self reset];
//Checking the internet connection
if ([self checkNetworkStatus] == NO){
if (self.delegate != nil && [self.delegate respondsToSelector:@selector(requestFailed:)]){
[self.delegate performSelector:@selector(requestFailed:) withObject:nil];
}
return;
}
//Sending the http requqest
NSMutableURLRequest *request = [self makeRequest:url];
[self prepareWithRequest:request];
if(![[url lastPathComponent] isEqualToString:@"pda_errorlogs"])
self.previousRequest = [NSMutableString string];
[self.previousRequest appendFormat:@"GET Requested API - %@\n",url];
[self.previousRequest appendFormat:@"\n\n\n"];
connection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];
}
/* Sending the Http Request for "POST" */
- (void)requestPOST:(NSString*)url body:(NSString*)body type:(NSString*)type {
//Reseting the http client
[self reset];
//Checking the internet connection
if ([self checkNetworkStatus] == NO){
if (self.delegate != nil && [self.delegate respondsToSelector:@selector(requestFailed:)]){
[self.delegate performSelector:@selector(requestFailed:) withObject:nil];
}
return;
}
//Sending the http requqest
NSMutableURLRequest *request = [self makeRequest:url];
[request setHTTPMethod:@"POST"];
if (type != nil && ![type isEqualToString:@""])
[request setValue:type forHTTPHeaderField:@"Content-Type"];
if (body) {
[request setHTTPBody:[body dataUsingEncoding:NSUTF8StringEncoding]];
}
[self prepareWithRequest:request];
if(![[url lastPathComponent] isEqualToString:@"pda_errorlogs"])
self.previousRequest = [NSMutableString string];
[self.previousRequest appendFormat:@"Post Requested API - %@\n",url];
[self.previousRequest appendFormat:@"Post Data - %@\n",body];
[self.previousRequest appendFormat:@"\n\n\n"];
connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
}
/* Sending the Http Request for "GET" with username and password */
- (void)requestGET:(NSString*)url username:(NSString*)username password:(NSString*)password {
//Reseting the http client
[self reset];
//Checking the internet connection
if ([self checkNetworkStatus] == NO){
if (self.delegate != nil && [self.delegate respondsToSelector:@selector(requestFailed:)]){
[self.delegate performSelector:@selector(requestFailed:) withObject:nil];
}
return;
}
//Sending the http requqest
NSMutableURLRequest *request = [self makeRequest:url username:username password:password];
if(![[url lastPathComponent] isEqualToString:@"pda_errorlogs"])
self.previousRequest = [NSMutableString string];
[self.previousRequest appendFormat:@"Post Requested API - %@\n",url];
[self.previousRequest appendFormat:@"Authorization user - %@\n",username];
[self.previousRequest appendFormat:@"Authorizating password - %@\n",password];
[self.previousRequest appendFormat:@"\n\n\n"];
connection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];
}
/* Sending the Http Request for "POST" with username and password */
- (void)requestPOST:(NSString*)url username:(NSString*)username password:(NSString*)password body:(NSString*)body type:(NSString*)type {
//Reseting the http client
[self reset];
//Checking the internet connection
if ([self checkNetworkStatus] == NO){
if (self.delegate != nil && [self.delegate respondsToSelector:@selector(requestFailed:)]){
[self.delegate performSelector:@selector(requestFailed:) withObject:nil];
}
return;
}
//Sending the http requqest
NSMutableURLRequest *request = [self makeRequest:url username:username password:password];
[request setHTTPMethod:@"POST"];
if (type != nil && ![type isEqualToString:@""])
[request setValue:type forHTTPHeaderField:@"Content-Type"];
if (body) {
[request setHTTPBody:[body dataUsingEncoding:NSUTF8StringEncoding]];
}
[self prepareWithRequest:request];
if(![[url lastPathComponent] isEqualToString:@"pda_errorlogs"])
self.previousRequest = [NSMutableString string];
[self.previousRequest appendFormat:@"Post Requested API - %@\n",url];
[self.previousRequest appendFormat:@"Authorization user - %@\n",username];
[self.previousRequest appendFormat:@"Authorizating password - %@\n",password];
[self.previousRequest appendFormat:@"Post Data - %@\n",body];
[self.previousRequest appendFormat:@"\n\n\n"];
connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
}
/* Sending the Http Request for "POST" with username and password */
- (void)requestPOST:(NSString*)url username:(NSString*)username password:(NSString*)password bodydata:(NSData*)body contenttype:(NSString*)type {
//Reseting the http client
[self reset];
//Checking the internet connection
if ([self checkNetworkStatus] == NO){
if (self.delegate != nil && [self.delegate respondsToSelector:@selector(requestFailed:)]){
[self.delegate performSelector:@selector(requestFailed:) withObject:nil];
}
return;
}
//Sending the http requqest
NSMutableURLRequest *request = [self makeRequest:url username:username password:password];
[request setHTTPMethod:@"POST"];
if (type != nil && ![type isEqualToString:@""])
[request setValue:type forHTTPHeaderField:@"Content-Type"];
if (body) {
[request setHTTPBody:body];
}
if (body != nil && [body length] > 0){
NSString* length_str = [NSString stringWithFormat:@"%d", [body length]];
[request setValue:length_str forHTTPHeaderField:@"Content-Length"];
}
[self prepareWithRequest:request];
if(![[url lastPathComponent] isEqualToString:@"pda_errorlogs"])
self.previousRequest = [NSMutableString string];
[self.previousRequest appendFormat:@"Post Requested API - %@\n",url];
[self.previousRequest appendFormat:@"Authorization user - %@\n",username];
[self.previousRequest appendFormat:@"Authorizating password - %@\n",password];
[self.previousRequest appendFormat:@"Post Data - %@\n",[[[NSString alloc] initWithData:body encoding:NSUTF8StringEncoding]autorelease]];
[self.previousRequest appendFormat:@"\n\n\n"];
connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
}
/* Sending the Http Request for uploading the image from database */
- (void)uploadImage:(NSString*)requesturl
username:(NSString*)username
password:(NSString*)password
imagename:(NSString*)imagename
contenttype:(NSString*)contenttype
imagedata:(NSData*)imagedata {
//Reseting the http client
[self reset];
//Checking the internet connection
if ([self checkNetworkStatus] == NO){
if (self.delegate != nil && [self.delegate respondsToSelector:@selector(requestFailed:)]){
[self.delegate performSelector:@selector(requestFailed:) withObject:nil];
}
return;
}
//Sending the http requqest
NSString *boundary = @"---------------------------14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
NSURL *url = [NSURL URLWithString:requesturl];
NSMutableURLRequest *request = [self makeRequest:requesturl username:username password:password];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];
NSMutableData *postbody = [NSMutableData data];
[postbody appendData:[[NSString stringWithFormat:@"--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"uploadfile\"; filename=\"%@\"\r\n", imagename] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
NSData* encoded_data = [Base64 encode:imagedata];
[postbody appendData:[NSData dataWithData:encoded_data]];
[postbody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setURL:url];
[request setHTTPMethod:@"POST"];
[encoded_data release];
[request setHTTPBody:postbody];
connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
}
/* Canceling the HTTP Transaction */
- (void)cancelTransaction {
[connection cancel];
[self reset];
}
#pragma mark -
#pragma mark NSURLConnectionDelegate methods
- (NSCachedURLResponse *)connection:(NSURLConnection *)connection willCacheResponse:(NSCachedURLResponse *)cachedResponse {
return nil;
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
statusCode = [(NSHTTPURLResponse*)response statusCode];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[recievedData appendData:data];
#ifdef _DEBUG
NSLog(@"Receieved the http body data : \n%@\n", [[[NSString alloc] initWithData:data
encoding:NSASCIIStringEncoding] autorelease]);
#endif
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
#ifdef DEBUG
NSLog(@"Receieved the http body data : \n%@\n", [[[NSString alloc] initWithData:recievedData
encoding:NSASCIIStringEncoding] autorelease]);
#endif
if (self.delegate != nil && [self.delegate respondsToSelector:@selector(requestSucceeded)]){
[self.delegate performSelector:@selector(requestSucceeded) withObject:nil];
}
[self reset];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError*) error {
#ifdef _DEBUG
NSLog(@"didFailWithError \n %@",[error description]);
#endif
if (self.delegate != nil && [self.delegate respondsToSelector:@selector(requestFailed:)]){
[self.delegate performSelector:@selector(requestFailed:) withObject:error];
}
[self reset];
}
//check network status chages
- (BOOL)checkNetworkStatus {
BOOL result = NO;
Reachability* reachability = [Reachability reachabilityWithHostName:SERVER_ADDRESS];
NetworkStatus remoteHostStatus = [reachability currentReachabilityStatus];
if(remoteHostStatus == NotReachable) {
#ifdef _DEBUG
NSLog(@"\nNetwork Status : not reachable\n");
#endif
result = NO;
}else if (remoteHostStatus == ReachableViaWWAN) {
#ifdef _DEBUG
NSLog(@"\nNetwork Status : reachable via wwan\n");
#endif
result = YES;
}else if (remoteHostStatus == ReachableViaWiFi) {
#ifdef _DEBUG
NSLog(@"\nNetwork Status : reachable via wifi\n");
#endif
result = YES;
}
return result;
}
@结束
如果有人能指导我正确的方向,那将有很大帮助。该项目使用 SDK 6.1 和部署目标 4.3。
//编辑我的问题
目前实现如下
@interface HttpClient_iPhone : NSObject{
NSURLConnection *connection;
NSMutableData *recievedData;
int statusCode;
id delegate;
Reachability* hostReachable;
BOOL networkChecked;
}
@property (retain,atomic) NSMutableString *previousRequest;
@property (retain,readonly) NSMutableData *recievedData;
@property (readonly) int statusCode;
@property (nonatomic, assign) id delegate;
现在对应的.m是
@implementation HttpClient_iPhone
@synthesize recievedData, statusCode;
@synthesize delegate,previousRequest;
- (id)init {
if (self = [super init]) {
[self reset];
delegate = nil;
networkChecked = NO;
}
return self;
}
- (void)dealloc {
if (connection) {
[connection cancel];
}
[connection release];
connection = nil;
if (recievedData) {
[recievedData release];
recievedData = nil;
}
[super dealloc];
}
- (void)reset {
if (recievedData) {
[recievedData release];
recievedData = nil;
}
recievedData = [[NSMutableData alloc] init];
if (connection) {
[connection cancel];
[connection release];
connection = nil;
}
statusCode = 0;
networkChecked = NO;
}
- (void)requestGET:(NSString*)url {
//Reseting the http client
[self reset];
//Checking the internet connection
if ([self checkNetworkStatus] == NO){
if (self.delegate != nil && [self.delegate respondsToSelector:@selector(requestFailed:)]){
[self.delegate performSelector:@selector(requestFailed:) withObject:nil];
}
return;
}
NSMutableURLRequest *request = [self makeRequest:url];
[self prepareWithRequest:request];
if(![[url lastPathComponent] isEqualToString:@"pda_errorlogs"])
self.previousRequest = [NSMutableString string];
[self.previousRequest appendFormat:@"GET Requested API - %@\n",url];
[self.previousRequest appendFormat:@"\n\n\n"];
connection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];
}
- (void)requestPOST:(NSString*)url body:(NSString*)body type:(NSString*)type {
[self reset];
if ([self checkNetworkStatus] == NO){
if (self.delegate != nil && [self.delegate respondsToSelector:@selector(requestFailed:)]){
[self.delegate performSelector:@selector(requestFailed:) withObject:nil];
}
return;
}
NSMutableURLRequest *request = [self makeRequest:url];
[request setHTTPMethod:@"POST"];
if (type != nil && ![type isEqualToString:@""])
[request setValue:type forHTTPHeaderField:@"Content-Type"];
if (body) {
[request setHTTPBody:[body dataUsingEncoding:NSUTF8StringEncoding]];
}
[self prepareWithRequest:request];
if(![[url lastPathComponent] isEqualToString:@"pda_errorlogs"])
self.previousRequest = [NSMutableString string];
[self.previousRequest appendFormat:@"Post Requested API - %@\n",url];
[self.previousRequest appendFormat:@"Post Data - %@\n",body];
[self.previousRequest appendFormat:@"\n\n\n"];
connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
}
- (void)requestGET:(NSString*)url username:(NSString*)username password:(NSString*)password {
[self reset];
if ([self checkNetworkStatus] == NO){
if (self.delegate != nil && [self.delegate respondsToSelector:@selector(requestFailed:)]){
[self.delegate performSelector:@selector(requestFailed:) withObject:nil];
}
return;
}
NSMutableURLRequest *request = [self makeRequest:url username:username password:password];
if(![[url lastPathComponent] isEqualToString:@"pda_errorlogs"])
self.previousRequest = [NSMutableString string];
[self.previousRequest appendFormat:@"Post Requested API - %@\n",url];
[self.previousRequest appendFormat:@"Authorization user - %@\n",username];
[self.previousRequest appendFormat:@"Authorizating password - %@\n",password];
[self.previousRequest appendFormat:@"\n\n\n"];
connection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];
}
- (void)requestPOST:(NSString*)url username:(NSString*)username password:(NSString*)password body:(NSString*)body type:(NSString*)type {
[self reset];
if ([self checkNetworkStatus] == NO){
if (self.delegate != nil && [self.delegate respondsToSelector:@selector(requestFailed:)]){
[self.delegate performSelector:@selector(requestFailed:) withObject:nil];
}
return;
}
NSMutableURLRequest *request = [self makeRequest:url username:username password:password];
[request setHTTPMethod:@"POST"];
if (type != nil && ![type isEqualToString:@""])
[request setValue:type forHTTPHeaderField:@"Content-Type"];
if (body) {
[request setHTTPBody:[body dataUsingEncoding:NSUTF8StringEncoding]];
}
[self prepareWithRequest:request];
if(![[url lastPathComponent] isEqualToString:@"pda_errorlogs"])
self.previousRequest = [NSMutableString string];
[self.previousRequest appendFormat:@"Post Requested API - %@\n",url];
[self.previousRequest appendFormat:@"Authorization user - %@\n",username];
[self.previousRequest appendFormat:@"Authorizating password - %@\n",password];
[self.previousRequest appendFormat:@"Post Data - %@\n",body];
[self.previousRequest appendFormat:@"\n\n\n"];
connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
}
- (void)requestPOST:(NSString*)url username:(NSString*)username password:(NSString*)password bodydata:(NSData*)body contenttype:(NSString*)type {
[self reset];
}
- (void)cancelTransaction {
[self reset];
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
statusCode = [(NSHTTPURLResponse*)response statusCode];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[recievedData appendData:data];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
if (self.delegate != nil && [self.delegate respondsToSelector:@selector(requestSucceeded)]){
[self.delegate performSelector:@selector(requestSucceeded) withObject:nil];
}
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError*) error {
if (self.delegate != nil && [self.delegate respondsToSelector:@selector(requestFailed:)]){
[self.delegate performSelector:@selector(requestFailed:) withObject:error];
}
}
//Crashlytics 上报告的崩溃日志图像
第 1 部分,共 2 部分
第 2 部分,共 2 部分
最佳答案
两条评论:
1) 将 header 中的 recievedData 属性类型更改为“保留”(即使它没有用作代码中的属性)
2) 在你释放一个NSURLConnection之前,你应该给它发送cancel
,即[connection cancel]
。您在 dealloc 和 reset 中释放它。此外,在您做的第一件事中取消它 - 不要事先释放它可能使用的数据。
编辑:根据您当前的代码,您的 reset
方法在修改可变数据之前仍然会取消。我修改了您的代码,如下所示,以反射(reflect)您的 dealloc
方法(您不需要 nil 语句):
- (void)reset {
if (connection) {
[connection cancel];
[connection release];
connection = nil;
}
if (recievedData) {
[recievedData release];
recievedData = nil;
}
recievedData = [[NSMutableData alloc] init];
statusCode = 0;
networkChecked = NO;
}
- (void)dealloc {
if (connection) {
[connection cancel];
[connection release];
}
if (recievedData) {
[recievedData release];
}
[super dealloc];
}
试试吧。
关于iphone - 为什么我在网络通信期间遇到随机崩溃 -(在 CFRelease 中崩溃)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16602738/
我让随机数低于之前的随机数。 if Airplane==1: while icounter0: print "You have enoph fuel to get to New
是否可以生成 BigFloat 的随机数?类型均匀分布在区间 [0,1)? 我的意思是,因为 rand(BigFloat)不可用,看来我们必须使用 BigFloat(rand())为了那个结局。然而,
我正在尝试学习 Kotlin,所以我正在学习互联网上的教程,其中讲师编写了一个与他们配合良好的代码,但它给我带来了错误。 这是错误 Error:(26, 17) Kotlin: Cannot crea
是否有任何方法可以模拟 Collections.shuffle 的行为,而不使比较器容易受到排序算法实现的影响,从而保证结果的安全? 我的意思是不违反类似的契约(Contract)等.. 最佳答案 在
我正在创建一个游戏,目前必须处理一些math.random问题。 我的Lua能力不是那么强,你觉得怎么样 您能制定一个使用 math.random 和给定百分比的算法吗? 我的意思是这样的函数: fu
我想以某种方式让按钮在按下按钮时随机改变位置。我有一个想法如何解决这个问题,其中一个我在下面突出显示,但我已经认为这不是我需要的。 import javafx.application.Applicat
对于我的 Java 类(class),我应该制作一个随机猜数字游戏。我一直陷入过去几天创建的循环中。程序的输出总是无限循环,我不明白为什么。非常感谢任何帮助。 /* This program wi
我已经查看了涉及该主题的一些其他问题,但我没有在任何地方看到这个特定问题。我有一个点击 Web 元素的测试。我尝试通过 ID 和 XPath 引用它,并使用 wait.until() 等待它变得可见。
我在具有自定义类的字典和列表中遇到了该异常。示例: List dsa = (List)Session["Display"]; 当我使用 Session 时,转换工作了 10-20 次..然后它开始抛
需要帮助以了解如何执行以下操作: 每隔 2 秒,这两个数字将生成包含从 1 到 3 的整数值的随机数。 按下“匹配”按钮后,如果两个数字相同,则绿色标签上的数字增加 1。 按下“匹配”按钮后,如果两个
void getS(char *fileName){ FILE *src; if((src = fopen(fileName, "r")) == NULL){ prin
如果我有 2 个具有以下字段的 MySQL 数据库... RequestDB: - Username - Category DisplayDB: - Username - Category
我有以下语句 select random() * 999 + 111 from generate_series(1,10) 结果是: 690,046183290426 983,732229881454
我有一个使用 3x4 CSS 网格构建的简单网站。但出于某种原因,当我在 chrome“检查”中检查页面时,有一个奇怪的空白 显然不在我的代码中的标签。 它会导致网站上出现额外的一行,从而导致出现
我有两个动画,一个是“过渡”,它在悬停时缩小图像,另一个是 animation2,其中图像的不透明度以周期性间隔重复变化。 我有 animation2 在图像上进行,当我将鼠标悬停在它上面时,anim
如图所示post在 C++ 中有几种生成随机 float 的方法。但是我不完全理解答案的第三个选项: float r3 = LO + static_cast (rand()) /( static_c
我正在尝试将类添加到具有相同类的三个 div,但我不希望任何被添加的类重复。 我有一个脚本可以将一个类添加到同时显示的 1、2 或 3 个 div。期望的效果是将图像显示为背景图像,并且在我的样式表中
我有一个基本上可以工作的程序,它创建由用户设置的大小的嵌套列表,并根据用户输入重复。 但是,我希望各个集合仅包含唯一值,目前这是我的输出。 > python3 testv.py Size of you
我正在尝试基于 C# 中的种子生成一个数字。唯一的问题是种子太大而不能成为 int32。有什么方法可以像种子一样使用 long 吗? 是的,种子必须很长。 最佳答案 这是我移植的 Java.Util.
我写这个函数是为了得到一个介于 0 .. 1 之间的伪随机 float : float randomFloat() { float r = (float)rand()/(float)RAN
我是一名优秀的程序员,十分优秀!