I have the codeblock like this and I am trying to get rid of the Float should be Int and Missing Return errors.
package com.bykd.dev;
#:final class Version
{
public static inline var SPLIT_CHAR : String = ".";
public static var revisionKeyword : String = "Revision";
private var _tag : String;
private var _numbers : Array<Dynamic>;
public static function create(pfx : String, rev : String = null, sfx : String = null) : Version
{
var nums : Array<Dynamic> = null;
nums = pfx.split(SPLIT_CHAR);
if (rev != null)
{
nums.push(trimRevision(rev));
}
return new Version(nums, sfx);
private static function trimRevision(rev : String) : String
{
var beg : Float = Math.NaN;
var end : Float = Math.NaN;
beg = Std.string("$" + revisionKeyword + ": ").length;
end = rev.lastIndexOf(" $");
return rev.substring(beg, end);
}
}
Errors are in the last lines :
end = rev.lastIndexOf(" $");
return rev.substring(beg, end);
Any help would be highly appreciated.
Why use Float?
var beg : Int = 0;
var end : Int = 0;
Also avoid Dynamic when possible
var nums : Array<String> = null;
nums = pfx.split(SPLIT_CHAR);
Related
I am trying to get the LatLngs so I can find the nearest place of the search result, I am trying it this way, but the getLatLngs returns null because it hasn't yet initialized.How do I access stuff from MainActivity after the onPostExecute is done.Thank you in advance.
public class GetNearbyPlaces extends AsyncTask<Object, String, String> {
private ArrayList<LatLng> LatLngs = new ArrayList<>();
private LatLng latLngMain;
private LatLng shortestLatLng;
#Override
protected void onPostExecute(String s) {
List<HashMap<String, String>> nearbyPlacesList = null;
DataParser dataParser = new DataParser();
nearbyPlacesList = dataParser.parse(s);
shortestLatLng = DisplayNearbyPlaces(nearbyPlacesList);
}
private LatLng DisplayNearbyPlaces(List<HashMap<String, String>> nearbyPlacesList)
{
int index = 0;
LatLng shortest;
double shortestDistance = Double.MAX_VALUE;
for(int i = 0;i<nearbyPlacesList.size();i++)
{
MarkerOptions markerOptions = new MarkerOptions();
HashMap<String, String> googleNearbyPlace = nearbyPlacesList.get(i);
String nameOfPlace = googleNearbyPlace.get("place_name");
String vicinity = googleNearbyPlace.get("vicinity");
double lat = Double.parseDouble(googleNearbyPlace.get("lat"));
double lng = Double.parseDouble(googleNearbyPlace.get("lng"));
LatLng latLng = new LatLng(lat,lng);
LatLngs.add(latLng);
markerOptions.position(latLng);
markerOptions.title(nameOfPlace + " : " + vicinity);
markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED));
if(mMap!=null)
mMap.addMarker(markerOptions);
}
for(int i = 0;i<LatLngs.size();i++)
{
double deltaLat = latLngMain.latitude - LatLngs.get(i).latitude;
double deltaLng = latLngMain.longitude - LatLngs.get(i).longitude;
double distanceInDegree = Math.sqrt(deltaLat * deltaLat + deltaLng * deltaLng);
double distanceInMeter = distanceInDegree * 4000;
if(distanceInMeter<shortestDistance)
{
shortestDistance=distanceInMeter;
index = i;
}
}
shortest = LatLngs.get(index);
return shortest;
}
public LatLng getShortestLatLng()
{
return shortestLatLng;
}
}
In MainActivity
getNearbyPlaces.execute(transferData).get();
nearestDistance = getNearbyPlaces.getShortestLatLng();
You must initialize your LatLngs field before calling methods on it.
Replace
private ArrayList<LatLng> LatLngs;
to
private ArrayList<LatLng> LatLngs = new ArrayList<>();
In your code, you initialize it only in loop of DisplayNearbyPlaces method, and if, for example,
nearbyPlacesList is empty, then body of this loop does not run and field remains uninitialized.
Trying to fix this error as I mentioned in title but cant figure it out. I am not comparing anything why I get this I don't know. I tried to change strings to float but no success. Maybe I should change StringTools but not sure what use instead of that.
package com.bykd.output;
#:final class DateOutput
{
public static inline var HOUR : String = "%h";
public static inline var MONTH : String = "%m";
public static inline var SECOND : String = "%s";
public static inline var MINUTE : String = "%i";
public static inline var DAY : String = "%d";
public static inline var YEAR : String = "%y";
public function new()
{
//super();
}
public static function formatDate(date : Date, format : String) : String
{
var output : String = null;
output = format;
output = StringTools.replace(output, DAY, leadZero(date.getDay));
output = StringTools.replace(output, MONTH, leadZero(date.getMonth));
output = StringTools.replace(output, YEAR, date.getFullYear);
return output;
}
public static function formatTime(date : Date, format : String) : String
{
var output : String = null;
output = format;
output = StringTools.replace(output, HOUR, leadZero(date.getHours));
output = StringTools.replace(output, MINUTE, leadZero(date.getMinutes));
output = StringTools.replace(output, SECOND, leadZero(date.getSeconds));
return output;
}
public static function leadZero(num : Float) : String
{
return Std.string("00" + num).substr(-2, 2);
}
}
This script should spawn object after clicking on other object which is closer than hitRange and has tag "Block". The problem is that it always spawns new object after pushing LMB, also when needed conditions weren't met.
#pragma strict
var blockName : String;
private static var blockToPlace : Rigidbody;
var hitRange : float;
private var hit : RaycastHit;
var blockLayer : int = 8;
private var hitBlock : boolean;
function Update(){
if(Input.GetMouseButtonDown(0)){
blockToPlace = EditorUtility.InstantiatePrefab(AssetDatabase.LoadAssetAtPath("Assets/Models/Blocks/"+blockName+".prefab", Rigidbody)) as Rigidbody;
hitBlock = Physics.Raycast(transform.position, transform.forward, hit, Mathf.Infinity, blockLayer);
if(hitBlock == true){
if(hit.collider.tag == "Block"){
if(hit.distance < hitRange){
var block : Rigidbody;
block = Instantiate(blockToPlace,hit.normal+hit.transform.position,Quaternion.identity) as Rigidbody;
hitBlock = false;
}
}
}
}
}
I had a hard time with the same problem myself. Layers arn't exactly integers but rather bitshifts.
Watch this very nice video on layermasks: www.youtube.com/watch?v=n8mAjwn-MB8
var blockLayer : int = 8;
should be
var blockLayer : int = 1 << 8;
Please could you help me here? I need to understand how to convert a String into an Int, Float or Double! This problem occurs when I'm trying to get the value from an UITextField and need this type of conversion!
I used to do it like this:
var myValue : Float = myTextField.text.bridgeToObjectiveC().floatValue
but since Xcode 6 beta 6 it doesn't seem to work anymore!
I've tried also like this:
var str = "3.14"
// Conversion from StringValue to an Int
var intValue : Int = str.toInt()!
// Other converstion from StringValue to an Int
var intOtherValue : Int = Int(str)
// Converstion from StringValue to a Float
var floatValue : Float = str.bridgeToObjectiveC().floatValue
// Converstion from StringValue to a Double
var doubleValue : Double = Double(str)
Please help me or tell me where I can find the answer! Many thanks!
Convert String to NSString and Use convenience methods:
var str = "3.1"
To Int
var intValue : Int = NSString(string: str).integerValue // 3
To Float
var floatValue : Float = NSString(string: str).floatValue // 3.09999990463257
To Double
var doubleValue : Double = NSString(string: str).doubleValue // 3.1
Reference
var doubleValue: Double { get }
var floatValue: Float { get }
var intValue: Int32 { get }
#availability(OSX, introduced=10.5)
var integerValue: Int { get }
#availability(OSX, introduced=10.5)
var longLongValue: Int64 { get }
#availability(OSX, introduced=10.5)
Use:
Int(string:String)
Double(string:String)
Float(string:String)
Which return an optional which is nil if it fails to parse the string.
For example:
var num = 0.0
if let unwrappedNum = Double("5.0") {
num = unwrappedNum
} else {
print("Error converting to Double")
}
Of course you can force unwrap if you are sure:
var foo = Double("5.0")!
Extending String
If you are doing this in more than a few places, and want error handling to be handled the same everywhere then you may want to extend String with conversion methods:
For example:
extension String {
func toDouble() -> Double {
if let unwrappedNum = Double(self) {
return unwrappedNum
} else {
// Handle a bad number
print("Error converting \"" + self + "\" to Double")
return 0.0
}
}
}
and then to use it:
let str = "4.9"
var num = str.toDouble()
public extension String {
public func toFloat() -> Float? {
return Float.init(self)
}
public func toDouble() -> Double? {
return Double.init(self)
}
}
var holdTextFieldToStringValue = myTextField.text
//convert from string to Int
var holdIntValue = holdTextFieldToStringValue.toInt()!
//convert from string to Double
var holdDoubleValue = Double((holdTextFieldToStringValue as NSString).doubleValue)
let strValue = "14.03"
let result = (strValue as NSString).floatValue
one class sample : testHTTP.class in this code
public String httptest(object httptestx)
{
var data = (Tuple<string, string, string>)httptestx;
var s = data.Item1;
var k = data.Item2;
var p = data.Item3;
............................
}
Form1 thread class not start please help me ... :
private void button5_Click(object sender, EventArgs e)
{
for (Int32 i5 = 0; i5 < textBox5.Lines.Length; i5++)
{
var thr1 = new Thread(dfproJesiHttpClass.httptest());
var data = new Tuple<string, string, string>(textBox6.Lines[i6].Trim(), textBox4.Lines[i4].Trim(), textBox5.Lines[i5].Trim());
thr1.Start(data);
threads.Add(thr1);
}
}
I'd be surprised if that code compiles. You need to change this line:
var thr1 = new Thread(dfproJesiHttpClass.httptest());
to
var thr1 = new Thread(dfproJesiHttpClass.httptest);
Assuming that dfproJesiHttpClass is an instance and not the name of the class.